1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#Enter a path to your import CSV file $ADUsers = Import-Csv "\\Server_Name\c$\Scripts\AD\users\AD01.csv" foreach ($User in $ADUsers) { $Username = $User.username $Password = $User.password $Firstname = $User.firstname $Lastname = $User.lastname $Department = $User.department $OU = $User.ou $Company = $user.Company $office = $user.office $description = $user.description $extensionAttribute1 = $user.extensionAttribute1 $extensionAttribute2 = $user.extensionAttribute2 $extensionAttribute3 = $user.extensionAttribute3 $extensionAttribute4 = $user.extensionAttribute4 $extensionAttribute5 = $user.extensionAttribute5 $extensionAttribute6 = $user.extensionAttribute6 $extensionAttribute7 = $user.extensionAttribute7 $extensionAttribute8 = $user.extensionAttribute8 $extensionAttribute9 = $user.extensionAttribute9 #Check if the user account already exists in AD if (Get-ADUser -F {SamAccountName -eq $Username}) { #If user does exist, output a warning message Write-Warning "A user account $Username has already exist in Active Directory." } else { #If a user does not exist then create a new user account #Account will be created in the OU listed in the $OU variable in the CSV file; don’t forget to change the domain name in the"-UserPrincipalName" variable New-ADUser ` -SamAccountName $Username ` -UserPrincipalName "$Username@domain.co.il" ` -Name "$Firstname $Lastname" ` -GivenName $Firstname ` -Surname $Lastname ` -Enabled $True ` -ChangePasswordAtLogon $True ` -DisplayName "$Firstname, $Lastname" ` -office $office ` -description $description ` -Department $Department ` -Path $OU ` -AccountPassword (convertto-securestring $Password -AsPlainText -Force) set-aduser -Identity $Username -add @{"extensionattribute1" =$($user.extensionattribute1)} set-aduser -Identity $Username -add @{"extensionattribute2" =$($user.extensionattribute2)} set-aduser -Identity $Username -add @{"extensionattribute3" =$($user.extensionattribute3)} set-aduser -Identity $Username -add @{"extensionattribute4" =$($user.extensionattribute4)} set-aduser -Identity $Username -add @{"extensionattribute5" =$($user.extensionattribute5)} set-aduser -Identity $Username -add @{"extensionattribute6" =$($user.extensionattribute6)} set-aduser -Identity $Username -add @{"extensionattribute7" =$($user.extensionattribute7)} set-aduser -Identity $Username -add @{"extensionattribute8" =$($user.extensionattribute8)} set-aduser -Identity $Username -add @{"extensionattribute9" =$($user.extensionattribute9)} } } |