• ARTICLES
SEARCH

How-To Geek

Create Multiple Users in Server 2008 with PowerShell, Part 2: More Fields

In our previous article, we showed you the fastest way to go from a simple list of users to their creation in Active Directory. However, you’ll frequently get a list of users that will have extra data fields. Since we can’t write a script ahead of time for every possible scenario, we’ll show you how to take our user creation PowerShell script and modify it to suit your purpose.

In this case, we’ve been given the Office attribute for our new users, so we’ll want to make sure that’s added in each of our new users account on creation.

sshot-2009-12-16-12-07-19

The first thing we do is to save the excel file as a .csv file.

005

Next we open up the Active Directory Users and Computers snap-in. We need to find out what is the actual attribute name for the Office field, so we’ll open the properties of one of our current users by double-clicking on them.

sshot-2009-12-15-18-59-53

We can see that this user has Human Resources filled in for his Office attribute on the General tab.

sshot-2009-12-16-11-33-08

We’ll click on the Atribute Editor tab to find out the technical name for that Office field, because we are going to need this to enter into PowerShell in a minute. It shows that the Attribute name for the Office field is physicalDeliveryOfficeName. Since the Attribute name is not always the same as the Field name in the other tabs, it can be a little difficult sometimes to find what you are looking for. If you can’t seem to find the field you need, you can always create a dummy user and modify just that specific field to something unique, then go to the Attribute Editor tab and scroll down until you find it.

sshot-2009-12-16-11-34-24

Now we’ll need to edit our PowerShell script to reflect this new field. The format for this will be “AttributeName=dataRecord”. We want to make sure that we enter the field name correctly from our .csv file after the “$dataRecord.” entry. This section will pull the data from our users file:

$physicalDeliveryOfficeName=$dataRecord.Office

and this section of the script will put it into the new user objects:

$objUser.Put(“physicalDeliveryOfficeName”,$physicalDeliveryOfficeName)

The finished script will look like this:

sshot-2009-12-16-12-05-10

Make sure to save the script as a .ps1 file.

009

With our users list and our script in the C:UsersAdministrator folder, we right-click the script and choose Run with PowerShell.

013

When we jump back over to AD Users and Computers, we can see out new users created.

sshot-2009-12-16-12-10-01

Opening up one of our newly created users will show that the Office field is filled out with the data from our original users list.

sshot-2009-12-16-12-10-31

Creating multiple users with PowerShell is a very easy task, and with this knowledge at your fingertips, you’ll never sweat it again.

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 (3)

  1. Kumdist

    I tried adding both the Office and Description information from csv file using the script and it failed. Could you advise? The script points out no additional scripts after the office entry?? Thanks

  2. nat

    hi there

    I have setup two servers 2k8 enterprise I’m following trainsignal 70-640 course on ad. both servers are server 2008 rc2 180 eval copies.I have checked ad replication is working fine. I can’t create user using your script or the example they do in the video series.

    When i run the powershell it runs but doesn’t create any user I am logged in as administrator so its not a rights issue. i have edit your script

    $objOU=[ADSI]“LDAP://OU=NYUSERS,OU=Newyork,DC=globomantics,DC=com”
    $dataSource=import-csv “tiara.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 + “@globomantics.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()
    }

    It still fails if use the following batch file :

    dsadd user”cn=%1, ou=nyusers, ou=newyork, dc=globomantics, dc=com” -fn %2 -ln %3 -pwd P@ssw0rd -mustchpwd yes

    Any help will be gratefully recieved

    Nat

  3. shafeer

    Hey NAT, R u sure No error thrown while running both powershell cmdlet and dsa cmd?? Do post the error if any.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!