Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#Import the values for the users
$UserValues = Import-Csv -Path c:\UserCreation\Users.csv -Delimiter ";"#Create the user function so I will not repeat myselffunctioncreateUsers($FirstName, $LastName, $Password, $Department){
#The path for the user to be placed, in this scenario I put them in a department OU and Users
$OUPath = "OU=Users,OU=$Department,OU=Departments,DC=TSTDOMAIN,DC=COM"
#Convert the password to securestring
$AccountPassword = (ConvertTo-SecureString -AsPlainText $Password -Force)
#Create the Displayname, samaccountname, userprincipal name
$DisplayName = $FirstName+" "+$LastName
$SamAccountName = $FirstName+"."+$LastName
$UserPrincipalName = $SamAccountName+""
#Create the profile and home directory based
$ProfilePath = "\\\Users\$Department\$SamAccount\Profile"
$HomeDirectory = "\\\Users\$Department\$SamAccount\Home"
#Set the HomeDrive letter
$HomeDrive = "H:"
#Try to create the user
New-ADUser -Surname $Name -GivenName $Name -DisplayName $DisplayName -SamAccountName $SamAccount -Name $DisplayName -AccountPassword $AccountPassword -Path $OU -Enabled $true -UserPrincipalName $UserPrincipalName -ProfilePath $ProfilePath -HomeDirectory $HomeDirectory -Department $Department -HomeDrive $HomeDrive
#If there is an error, capture it and display the error
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage -ForegroundColor Red
#Notice that I use single instead of plural variable in the foreachforeach($UserValue in $UserValues){
#call the createUsers function to create the user
createUsers -FirstName $UserValue.FirstName -LastName $UserValue.LastName -Password $UserValue.Password -Department $UserValue.Department
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.