Use PowerShell to Create First and Last Names for Test Users

Use PowerShell to Create First and Last Names for Test Users

  • Comments 5
  • Likes

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • How to create a user in Powershell?

    Command to create a user on powershell, i found this language so easy to use but to

  • I have created a script (http://poshcode.org/3960) to get random name pairs from a website.
    It looks nicer than simple "Testuer1234" Accounts:

    example:

    $females = Get-RandomNames -MaxName 100 -Gender Female

    and create the female test users with this one liner:
    $females | foreach {new-aduser -name "$($($_.givenname).SubString(0,1))$($_.surname)" -displayname "$($_.surname), $($_.givenname)" -Path "ou=users,dc=domain,dc=local" -Department "Sales" -Enabled $True -Surname "$($_.surname)" -GivenName "$($_.givenname)" -AccountPassword (ConvertTo-SecureString "P@ssw0rd" -As PlainText -force)}



  • well, in real world it will be very hard to split it as some naming convention is first initial lastname if exist then append. but some naming convention is first character of first name and last name if exist do first character of first name 2nd letter of first name then lastname. and if the joke about it, if all users are Chinese last name then it works 95% as the are probably more than 5% of the Chinese has double vowel. What I am trying to say is in real world there is no way to split username into first and last name. The only way might be that if naming convention is first.last is the username which is probably the best naming convention.

  • sorry about my grammar on the last reply. Thanks Ed for writhing the tip. At least I learned that $fname.substring(0,1) is the first character of the string variable $fname. I found this post as I was find my way to do concatenate of variables and found nothing and this one was close. The substring. I am posting this for those who is following my foot steps to find an answer to wanting to concatenate first character of a firstname to last name.

    $fname = joe
    $lname = li
    $flname = $fname.substring(0,1) + lname


    "take and give, but some site won't let you give by requiring one to register." :)

  • thanks