Last active
September 15, 2021 21:42
-
-
Save gtwy/12325b48124eebea54307f4e6aff784e to your computer and use it in GitHub Desktop.
(Powershell) Create users in Active Directory and add Email Aliases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UT-8 CSV file should look as follows: | |
Display,First,Last,Email,Password,Aliases | |
John Smith,John,Smith,john.smith@contoso.com,TempPassword123,john@contoso.com | |
Jane Smith,Jane,Smith,jane.smith@contoso.com,TempPassword456, | |
Joe Sixpack,Joe,Sixpack,joe.sixpack@contoso.com,TempPassword789,"joe@contoso.com,beer@contoso.com" | |
Line 1: John Smith has 1 alias, john@contoso.com | |
Line 2: Jane has zero aliases | |
Line 3: Joe has two alieases separated by a comma, ergo the aliases column must be "quoted" | |
All three of these entries are valid. You can add as many aliases as you want as long as they are separated by a comma. | |
Excel will automatically export in this format if you convert an Excel file. | |
Save CSV file as "user-list.csv" and go to the directory with powershell as and administrator on the domain controller. | |
Paste into powershell: | |
import-csv -path .\user-list.csv | foreach { | |
echo "" | |
echo "" | |
Write-Host "Adding $($_.Display)" | |
echo "" | |
$samAccountName = $_.Email.split('@')[0] | |
$samDomain = $_.Email.split('@')[1] | |
$aliases = $_.Aliases.split(',') | |
$alias = @("SMTP:$($_.Email)") | |
foreach ($item in $aliases) { | |
if ($item.Length -gt 0) { | |
$alias += "smtp:$item" | |
} | |
} | |
new-aduser -name $samAccountName -enabled $true –givenName $_.First –surname $_.Last -accountpassword (convertto-securestring $_.Password -asplaintext -force) -changepasswordatlogon $false -samaccountname $samAccountName –userprincipalname $_.Email -emailaddress $_.Email -displayname $_.Display | |
Set-ADUser -Identity $samAccountName -Add @{proxyAddresses= $alias} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a version that also adds users to groups. Create a groups column at the end of user-list.csv. It works the same way as aliases, use quotes and separate with a comma if there are multiple groups.