How-To Geek
How to Create Multiple Users in Server 2008 with PowerShell
Creating users through the AD Users and Computers snap-in is a very easy process, but you’ll frequently face the situation where you need to create accounts for a whole group of people at once. There’s no need for this to be a time consuming process for you though, and we’ve done all the heavy lifting so you don’t have to.
We’ve just got a list of new employees from the HR Department, and they’ve been kind enough to give it to us in an excel format.

The first thing we are going to do is save the file as a .csv, and to do that, we click on the Office Button and select Save As.


We’re going to name our file users.csv, and make sure that we pick CSV (Comma delimited) in the Save as type box, and then click Save.


Next we’ll create a new text document on the server where we’ll be doing the user creation.


We’ll then copy the following into our new text document:
$objOU=[ADSI]“LDAP://OU=People,DC=sysadmingeek,DC=com”
$dataSource=import-csv “users.csv”
foreach($dataRecord in $datasource) {
$cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName
$sAMAccountName=$dataRecord.FirstName + “.” + $dataRecord.LastName
$givenName=$dataRecord.FirstName
$sn=$dataRecord.LastName
$sAMAccountName=$sAMAccountName.ToLower()
$displayName=$sn + “, ” + $givenName
$userPrincipalName=$sAMAccountName + “@sysadmingeek.com”
$objUser=$objOU.Create(“user”,”CN=”+$cn)
$objUser.Put(“sAMAccountName”,$sAMAccountName)
$objUser.Put(“userPrincipalName”,$userPrincipalName)
$objUser.Put(“displayName”,$displayName)
$objUser.Put(“givenName”,$givenName)
$objUser.Put(“sn”,$sn)
$objUser.SetInfo()
$objUser.SetPassword(“P@assw0rd”)
$objUser.psbase.InvokeSet(“AccountDisabled”,$false)
$objUser.SetInfo()
}
In the first line, make sure that you enter the correct information for your domain and the OU where you are creating the users.


We then want to save the file as a PowerShell script, so we change the Save as type: to All Files (*), and name it PSusersScript.ps1.


Now we need to prep PowerShell to run scripts. You can launch PowerShell by clicking on the shortcut in the taskbar, or by typing PowerShell in the quick search box.


We need to change the Execution Policy to allow scripts to be run remotely, so we type
set-executionpolicy remotesigned
When prompted, we type Y and then hit enter to execute.


Now that we’ve allowed the script to be run, we need to place both the users.csv and the PSusersScript.ps1 files in our folder for execution. Since the PowerShell prompt naturally comes up to the root user folder, and we are logged on as Administrator, we are going to place them in the C:UsersAdministrator folder. When both files are in the folder, we right-click on the PSusersScript.ps1 file and choose Run with PowerShell.


If we take a look in AD Users and Computers, you will now see all those new users you just created.


The new users will be created in the lastname.firstname format, but the script could easily be altered to your need. Now that you’ve already created the script, all you have to do in the future is to place your list of users in the C:UsersAdministrator folder and run the PowerShell script. Easy!
Got Feedback? Join the discussion at discuss.howtogeek.com
Comments (10)
Programmer by day, geek by night, The Geek, also known as Lowell Heddings, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on Google+ if you'd like.
- Published 12/16/09




i cannot seem to replicate this on a 2008 enterprise server
it just doesn’t create users (well actually i got it to work, somehow, ONCE and i am pretty sure it just randomly did this or something =) )
could you put the .csv file example up because it is really bugging me, not knowing what it should look like
Mine started like this
FirstName; LastName
SMITH;YOUNG
JOHNSON;WRIGHT
and so on….
is this right?
i really like this example but just cannot wrap my mind around how it should be done.
thank you
My attempt is failing. I cannot see what it is doing either because it closes the window too fast. Is there a way to have a log file created to figure where the failure is? I also need to add an additional data field so a user account will be something like jwilc334 for first initial first name, four of last name, and last three of lunch number.
i edited the .ps1 file so it would open the powershell editor and ran it there to see and copy the errors.
The scenario is too simple. What if I need the accounts to be assigned a password, made member of certain security group, create home directories, etc.? Can the same script be used with modification?
Thanks!
Shameem
Great script… I modified slightly to meet my needs and to provide a randomly generated password for all my users.
*PSUsersScript.ps1*
$objOU=[ADSI]“LDAP://OU=City Users,OU=SunDrink Users,DC=sundrink,DC=local”
$dataSource=import-csv “users.csv”
foreach($dataRecord in $datasource) {
$cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName
$sAMAccountName=$dataRecord.FirstName + “.” + $dataRecord.LastName
$givenName=$dataRecord.FirstName
$sn=$dataRecord.LastName
$sAMAccountName=$sAMAccountName.ToLower()
$displayName=$GivenName + ” ” + $Sn
$userPrincipalName=$sAMAccountName + “@piper.local”
$objUser=$objOU.Create(“user”,”CN=”+$cn)
$objUser.Put(“sAMAccountName”,$sAMAccountName)
$objUser.Put(“userPrincipalName”,$userPrincipalName)
$objUser.Put(“displayName”,$displayName)
$objUser.Put(“givenName”,$givenName)
$objUser.Put(“sn”,$sn)
$SetPassword=$dataRecord.Password
$objUser.SetInfo()
$objUser.(“SetPassword”,$SetPassword)
$objUser.psbase.InvokeSet(“AccountDisabled”,$false)
$objUser.SetInfo()
}
*users.csv*
FirstName,LastName,Password
Joe,Blow,m3yEDURr
Chris,Goat,cra2esTr
Bobby,Brown,s29PafrR
Matt,Johnson,5RexuspR
Nick,Noob,f8Swacar
Peter,Ramos,suW6uy3T
Rick,Gonzo,CHebra45
Sarah,Winner,tRA5ucR6
Sean,Rock,YAxUc3xr
Sergey,Moon,pHehu3u9
Ryan,Williams,drAt3amR
Scott,Glover,d77aswk
PS C:\Users\Administrator> .\PSusersScript
Exception calling “Create” with “2″ argument(s): “An invalid directory pathname was passed
”
At C:\Users\Administrator\PSusersScript.ps1:11 char:23
+ $objUser=$objOU.Create <<<< ("user","CN="+$cn)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\PSusersScript.ps1:12 char:13
+ $objUser.Put <<<< ("sAMAccountName",$sAMAccountName)
+ CategoryInfo : InvalidOperation: (Put:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\PSusersScript.ps1:13 char:13
+ $objUser.Put <<<< ("userPrincipalName",$userPrincipalName)
+ CategoryInfo : InvalidOperation: (Put:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\PSusersScript.ps1:14 char:13
+ $objUser.Put <<<< ("displayName",$displayName)
+ CategoryInfo : InvalidOperation: (Put:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\PSusersScript.ps1:15 char:13
+ $objUser.Put <<<< ("givenName",$givenName)
+ CategoryInfo : InvalidOperation: (Put:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\PSusersScript.ps1:16 char:13
+ $objUser.Put <<<< ("sn",$sn)
+ CategoryInfo : InvalidOperation: (Put:String) [], RuntimeException
the above is all I seem to get :/
I get the same as Dave O’Reilly
Its most likely becuase our server requires a Password of a certain strength etc to successfully create a user
Oh nvm I see there is a password assigned in that script :/
@ YY, How did you edit the ps1 to open Powershell? What is the string?