Hey all -
I have a working bulk new-aduser Powershell script. However we are adding in large batches and there are some duplicate names. Example - Testy McTesterson may be in one CSV, and Tested McTesterson may be in another. I'm trying to add the ability to append
numbers say 1,2,3 - up to 9 to the sam, upn. in case duplicates are encountered. Working script so far:
#Create New Users
$users = Import-Csv -path "C:\Example.csv"
foreach ($user in $users) {
#Defined hashtable to splat to New-ADUser
$pass = ConvertTo-SecureString "changeme" -AsPlainText -Force
$hash = @{
Office = "$($user.'physicaldeliveryofficename')"
DisplayName = "$($user.'displayname')"
Name = "$($user.'givenname') $($user.'sn')"
EmailAddress = "$($user.'givenname').$($user.'sn')@contoso.com"
UserPrincipalName = "$($user.'givenname') $($user.'sn')"
Samaccountname = "$(($user.givenname).substring(0,1))$($user.'sn')"
GivenName = $($user.'givenname')
SurName = $($user.'sn')
Path = "$($user.'destinationOU')"
AccountPassword = $pass
Enabled = $True
ChangePasswordAtLogon = $True
}
New-ADUser -Verbose @hash -PassThru
}