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.

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.