Automatically Create Users in AD

Here’s a PowerShell script I put together to import users into AD (Active Directory). You just need to edit the .CSV file to add your users into it, then simply run the script pointing to the .CSV file.

This is good for testing purposes.

Prior to running the script, ensure that you have added your email domains to AD as UPNs to the domain and also that you have extended the schema for Exchange, to add in the all the Exchange attributes.

It also supports multiple email domains, if you don’t have multiple email domain, hash out the third line and the second last line.

$OU = 'OU=Test Users,DC=test,DC=local'
$primaryemaildomain = ""
$secondaryemaildomain = ""
$users = Import-Csv C:usersAdministratorDesktopNew-ADUser.csv
$users | ForEach-Object {
$Name = $_.givenname + " " + $_.surname
$UPN = ($_.GivenName).ToLower().Chars(0) + $_.Surname.ToLower() + "@" + $primaryemaildomain
$SamAccountName = ($_.GivenName).ToLower().Chars(0) + $_.Surname.ToLower()
#$secondaryaddress = ($_.GivenName).ToLower().Chars(0) + $_.Surname.ToLower() + "@" + $secondaryemaildomain
$password = $_.AccountPassword
New-ADUser `
-GivenName $_.GivenName `
-Surname $_.Surname `
-Name $Name `
-DisplayName $name `
-SamAccountName $SamAccountName `
-Description $_.Description `
-UserPrincipalName $UPN `
-Title $_.Title `
-StreetAddress $_.StreetAddress `
-City $_.City `
-State $_.State `
-Country $_.Country `
-PostalCode $_.PostalCode `
-OfficePhone $_.OfficePhone `
-MobilePhone $_.MobilePhone `
-Department $_.Department `
-Office $_.Office `
-Path $OU `
-Enabled $True `
-AccountPassword (ConvertTo-SecureString $password -AsPlainText -force) `
-PasswordNeverExpires $True

$distinguishedName = "CN=" + $Name + "," + $ou
Set-ItemProperty -Path ad:$distinguishedName -Name mailnickname -Value $SamAccountName
Set-ItemProperty -Path ad:$distinguishedName -Name mail -Value $UPN
Set-ItemProperty -Path ad:$distinguishedName -Name proxyaddresses -Value SMTP:$UPN
Set-ADUser $DistinguishedName -Add @{proxyaddresses="smtp:$secondaryaddress"}

Here is the CSV file, copy and paste into a text file and rename to *.CSV.

Brad,Herald,Test User,,,Sydney,NSW,AU,2077,,,,,Passw0rd,$true
Frank,McDonald,Test User,,,Sydney,NSW,AU,2078,,,,,Passw0rd,$true
Jason,Freddo,Test User,,,Sydney,NSW,AU,2079,,,,,Passw0rd,$true
Amber,Parsons,Test User,,,Sydney,NSW,AU,2080,,,,,Passw0rd,$true
Amanda,Peters,Test User,,,Sydney,NSW,AU,2081,,,,,Passw0rd,$true
Marc,Brosnan,Test User,,,Sydney,NSW,AU,2082,,,,,Passw0rd,$true
Ranger,Due,Test User,,,Sydney,NSW,AU,2083,,,,,Passw0rd,$true
Sue,Murphy,Test User,,,Sydney,NSW,AU,2084,,,,,Passw0rd,$true
Nicola,Newland,Test User,,,Sydney,NSW,AU,2085,,,,,Passw0rd,$true
Peter,McKenzie,Test User,,,Sydney,NSW,AU,2086,,,,,Passw0rd,$true
Barbara,Walters,Test User,,,Sydney,NSW,AU,2087,,,,,Passw0rd,$true
Michael,Hound,Test User,,,Sydney,NSW,AU,2088,,,,,Passw0rd,$true
Jessica,Andrews,Test User,,,Sydney,NSW,AU,2089,,,,,Passw0rd,$true
Sarah,Cummings,Test User,,,Sydney,NSW,AU,2090,,,,,Passw0rd,$true

Here’s the finished result.