Use PowerShell to Create First and Last Names for Test Users

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create first and last names in Active Directory for test users.

Hey, Scripting Guy! Question Hey, Scripting Guy! I have a test environment set up, and I created a bunch of users in an organizational unit in Active Directory. But I only created the user name—I did not create the first and the last names of the users. Now I would like to do this, but I am afraid it will be too much work. Should I go back to my original test script, or do you know a shortcut?

—BW

Hey, Scripting Guy! Answer Hello BW,

Microsoft Scripting Guy, Ed Wilson, is here. Well, right now I am not drinking any tea. Nor coffee. Or even water. I am sitting here checking my email and checking out the latest build of Windows Server 2012 R2 and Windows 8.1. It is looking really good, and I cannot wait for RTM. There is some really cool stuff here.

Anyway, BW, I am going to make an assumption that you created your users by concatenating some characters.

Note  For a great example of how to create users in Active Directory for test purposes, see the Hey, Scripting Guy! Blog post, Create Test Users in a Test Active Directory Environment by Using PowerShell.

First, run the query

The first thing I do is run a query to see what I actually have in the target organizational unit (OU). I use the Get-ADUser cmdlet from the Active Directory module for this task. Here is the command and a sample of the output:

PS C:> get-aduser -filter * -SearchBase ‘ou=testou,dc=iammred,dc=net’

 

DistinguishedName : CN=newtestuser70,OU=Testou,DC=iammred,DC=net

Enabled           : False

GivenName         :

Name              : newtestuser70

ObjectClass       : user

ObjectGUID        : c688679b-5207-49c2-89f2-33dc74d1ea0d

SamAccountName    : newtestuser70

SID               : S-1-5-21-1457956834-3844189528-3541350385-1440

Surname           :

UserPrincipalName :

Note  The results that returned from Get-ADUser showed GivenName and Surname. In ADSI Edit, the attribute names are GivenName and SN.

All of the users are named newtestuser## (with a number at the end of the name). Therefore, I decide to split the name at the letter u. The first name (GivenName) will be NewTest for all of the users, and the last name (Surname) will be User##.

To do this, I use the Split command. An example of this technique is shown here:

PS C:> get-aduser -filter * -SearchBase ‘ou=testou,dc=iammred,dc=net’ | % {$_.name –

split ‘u’}

newtest

ser70

newtest

ser71

That works pretty well, but I want the last names to all be User## and not just Ser##. So I need to add back the letter “U.” I don’t feel like doing that. So I decide to try a different approach and use the Substring method from the String class. Because each user name is a string, I can call the Substring method directly. To get the first name, I start at position 0, and I count over 7 characters. This comprises the entire first name, as shown here:

PS C:> get-aduser -filter * -SearchBase ‘ou=testou,dc=iammred,dc=net’ | % {($_.name)

.substring(0,7)  }

newtest

newtest

And it is shown here for the last name:

PS C:> get-aduser -filter * -SearchBase ‘ou=testou,dc=iammred,dc=net’ | % {($_.name)

.substring(7)  }

user70

user71

The cool thing here is that the Substring method begins at the position 7 and continues to the end of the word. So it works no matter how many characters are in the user’s name.

Now to set the users’ first and last names

Now that I know the technique I will use, it is a matter of plug-and-play. Here is what I came up with (% is an alias for Foreach-Object):

get-aduser -filter * -SearchBase ‘ou=testou,dc=iammred,dc=net’ |

% {Set-ADUser -Identity $_ -GivenName ($_.name).substring(0,7)  -Surname ($_.name).substring(7)}

I check to ensure that it worked:

PS C:> get-aduser -filter * -SearchBase ‘ou=testou,dc=iammred,dc=net’

 

DistinguishedName : CN=newtestuser70,OU=Testou,DC=iammred,DC=net

Enabled           : False

GivenName         : newtest

Name              : newtestuser70

ObjectClass       : user

ObjectGUID        : c688679b-5207-49c2-89f2-33dc74d1ea0d

SamAccountName    : newtestuser70

SID               : S-1-5-21-1457956834-3844189528-3541350385-1440

Surname           : user70

UserPrincipalName :

 

DistinguishedName : CN=newtestuser71,OU=Testou,DC=iammred,DC=net

Enabled           : False

GivenName         : newtest

Name              : newtestuser71

ObjectClass       : user

ObjectGUID        : 3e0eb0b0-8ee7-49a1-96a2-bc20634f1648

SamAccountName    : newtestuser71

SID               : S-1-5-21-1457956834-3844189528-3541350385-1441

Surname           : user71

UserPrincipalName :

BW, that is all there is to using the Substring method to obtain the first and last names for test users in  Active Directory. Join me tomorrow when I will have a guest blog post by Jakob Gottlieb Svendsen. He will talk about a way cool WMI browser. You don’t want to miss it.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy