Skip to content

Instantly share code, notes, and snippets.

@marckean
Created June 7, 2017 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marckean/56c665b9d7286eac0700831068d1ce97 to your computer and use it in GitHub Desktop.
Save marckean/56c665b9d7286eac0700831068d1ce97 to your computer and use it in GitHub Desktop.
$OU = 'OU=Test Users,DC=test,DC=local'
$primaryemaildomain = "demo.domain.com"
$secondaryemaildomain = "demo2.domain.com"
$users = Import-Csv C:\users\Administrator\Desktop\New-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"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment