Import users from an Excel spreadsheet
Even if you don’t create new users in bulk often, when you have more
than one new user to deal with, importing from a comma-delimited file is
faster than using the GUI server administration tools or manually
typing PowerShell commands. Just fill out the fields in the spreadsheet,
run the script and the job is done.
Use an Excel spreadsheet to import new Active Directory users (Image: Russell Smith)
In the script below, I’ve used a simple
foreach loop and the
import-csv cmdlet to import and generate the new user accounts. The only additional code I’ve added is a split to separate users’
first and
second names in the .csv file’s
Name field, to populate the
–givenName and
–surname parameters, for the sake of completeness and to keep the number of columns in the .csv file to a minimum.
|
import-csv -path c:\temp\users.csv | foreach {
$givenName = $_.name.split()[0]
$surname = $_.name.split()[1]
new-aduser -name $_.name -enabled $true –givenName $givenName –surname $surname -accountpassword (convertto-securestring $_.password -asplaintext -force) -changepasswordatlogon $true -samaccountname $_.samaccountname –userprincipalname ($_.samaccountname+”@ad.contoso.com”) -city $_.city -department $_.department
}
|
Save the script as a .ps1 file. The default PowerShell script execution policy in Windows 8 is set to
Restricted, so if you’ve not run PowerShell scripts before, you might need to set the execution policy to
Remote Signed, which allows local unsigned scripts to run. Run
get-executionpolicy to see the current policy on your machine and
set-executionpolicy remotesigned to change the policy if necessary.
Now fill out the columns in the .csv file as shown in the image above as appropriate, save it and run the script.
Comments
Post a Comment