SEARCH

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.

001

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.

004

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.

005

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

007

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.

sshot-2009-12-15-21-45-57

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.

009

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.

011

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.

012

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.

013

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

0001

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!

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 12/16/09

Comments (10)

  1. yy

    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

  2. Jeremy M

    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.

  3. yy

    i edited the .ps1 file so it would open the powershell editor and ran it there to see and copy the errors.

  4. Shameem Murad

    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

  5. Robert Noblit

    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

  6. Dave O'Reilly

    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

  7. Dave O'Reilly

    the above is all I seem to get :/

  8. Christo

    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

  9. Christo

    Oh nvm I see there is a password assigned in that script :/

  10. Jeremy M

    @ YY, How did you edit the ps1 to open Powershell? What is the string?

Enter Your Email Here to Get Access for Free:

Go check your email!