{"id":3217,"date":"2013-07-19T00:01:00","date_gmt":"2013-07-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/07\/19\/use-powershell-to-create-first-and-last-names-for-test-users\/"},"modified":"2013-07-19T00:01:00","modified_gmt":"2013-07-19T00:01:00","slug":"use-powershell-to-create-first-and-last-names-for-test-users","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-first-and-last-names-for-test-users\/","title":{"rendered":"Use PowerShell to Create First and Last Names for Test Users"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create first and last names in Active Directory for test users.<\/span>\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;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&mdash;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?\n&mdash;BW\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello BW,\nMicrosoft 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&nbsp;2012&nbsp;R2 and Windows 8.1. It is looking really good, and I cannot wait for RTM. There is some really cool stuff here.\nAnyway, BW, I am going to make an assumption that you created your users by concatenating some characters.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note&nbsp;&nbsp;<\/strong>For a great example of how to create users in Active Directory for test purposes, see the Hey, Scripting Guy! Blog post, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/10\/30\/create-test-users-in-a-test-active-directory-environment-by-using-powershell.aspx\">Create Test Users in a Test Active Directory Environment by Using PowerShell<\/a>.<\/p>\n<h2>First, run the query<\/h2>\n<p>The first thing I do is run a query to see what I actually have in the target organizational unit (OU). I use the <strong>Get-ADUser<\/strong> cmdlet from the Active Directory module for this task. Here is the command and a sample of the output:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; get-aduser -filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">DistinguishedName : CN=newtestuser70,OU=Testou,DC=iammred,DC=net<\/p>\n<p style=\"padding-left: 30px\">Enabled&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : False<\/p>\n<p style=\"padding-left: 30px\">GivenName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : newtestuser70<\/p>\n<p style=\"padding-left: 30px\">ObjectClass&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : user<\/p>\n<p style=\"padding-left: 30px\">ObjectGUID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : c688679b-5207-49c2-89f2-33dc74d1ea0d<\/p>\n<p style=\"padding-left: 30px\">SamAccountName&nbsp;&nbsp;&nbsp; : newtestuser70<\/p>\n<p style=\"padding-left: 30px\">SID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : S-1-5-21-1457956834-3844189528-3541350385-1440<\/p>\n<p style=\"padding-left: 30px\">Surname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">UserPrincipalName :\n<strong>Note&nbsp;&nbsp;<\/strong>The results that returned from <strong>Get-ADUser<\/strong> showed <strong>GivenName<\/strong> and <strong>Surname<\/strong>. In ADSI Edit, the attribute names are <strong>GivenName<\/strong> and <strong>SN<\/strong>.\nAll of the users are named <strong>newtestuser##<\/strong> (with a number at the end of the name). Therefore, I decide to split the name at the letter u. The first name (<strong>GivenName<\/strong>) will be <strong>NewTest <\/strong>for all of the users, and the last name (<strong>Surname<\/strong>) will be<strong><em> <\/em>User##<\/strong>.<em> <\/em>\nTo do this, I use the <strong>Split<\/strong> command. An example of this technique is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; get-aduser -filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; | % {$_.name &#8211;<\/p>\n<p style=\"padding-left: 30px\">split &#8216;u&#8217;}<\/p>\n<p style=\"padding-left: 30px\">newtest<\/p>\n<p style=\"padding-left: 30px\">ser70<\/p>\n<p style=\"padding-left: 30px\">newtest<\/p>\n<p style=\"padding-left: 30px\">ser71\nThat works pretty well, but I want the last names to all be <strong>User##<\/strong> and not just <strong>Ser##<\/strong><em>.<\/em> So I need to add back the letter &ldquo;U.&rdquo; I don&rsquo;t feel like doing that. So I decide to try a different approach and use the <strong>Substring<\/strong><em> <\/em>method from the <strong>String<\/strong> class. Because each user name is a string, I can call the <strong>Substring<\/strong><em> <\/em>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:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; get-aduser -filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; | % {($_.name)<\/p>\n<p style=\"padding-left: 30px\">.substring(0,7)&nbsp; }<\/p>\n<p style=\"padding-left: 30px\">newtest<\/p>\n<p style=\"padding-left: 30px\">newtest\nAnd it is shown here for the last name:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; get-aduser -filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; | % {($_.name)<\/p>\n<p style=\"padding-left: 30px\">.substring(7)&nbsp; }<\/p>\n<p style=\"padding-left: 30px\">user70<\/p>\n<p style=\"padding-left: 30px\">user71\nThe cool thing here is that the Substring<em> <\/em>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&rsquo;s name.<\/p>\n<h2>Now to set the users&rsquo; first and last names<\/h2>\n<p>Now that I know the technique I will use, it is a matter of plug-and-play. Here is what I came up with (<strong>%<\/strong> is an alias for <strong>Foreach-Object<\/strong>):<\/p>\n<p style=\"padding-left: 30px\">get-aduser -filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; |<\/p>\n<p style=\"padding-left: 30px\">% {Set-ADUser -Identity $_ -GivenName ($_.name).substring(0,7)&nbsp; -Surname ($_.name).substring(7)}\nI check to ensure that it worked:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; get-aduser -filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">DistinguishedName : CN=newtestuser70,OU=Testou,DC=iammred,DC=net<\/p>\n<p style=\"padding-left: 30px\">Enabled&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : False<\/p>\n<p style=\"padding-left: 30px\">GivenName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : newtest<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : newtestuser70<\/p>\n<p style=\"padding-left: 30px\">ObjectClass&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : user<\/p>\n<p style=\"padding-left: 30px\">ObjectGUID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : c688679b-5207-49c2-89f2-33dc74d1ea0d<\/p>\n<p style=\"padding-left: 30px\">SamAccountName&nbsp;&nbsp;&nbsp; : newtestuser70<\/p>\n<p style=\"padding-left: 30px\">SID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : S-1-5-21-1457956834-3844189528-3541350385-1440<\/p>\n<p style=\"padding-left: 30px\">Surname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : user70<\/p>\n<p style=\"padding-left: 30px\">UserPrincipalName :<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">DistinguishedName : CN=newtestuser71,OU=Testou,DC=iammred,DC=net<\/p>\n<p style=\"padding-left: 30px\">Enabled&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : False<\/p>\n<p style=\"padding-left: 30px\">GivenName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : newtest<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : newtestuser71<\/p>\n<p style=\"padding-left: 30px\">ObjectClass&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : user<\/p>\n<p style=\"padding-left: 30px\">ObjectGUID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 3e0eb0b0-8ee7-49a1-96a2-bc20634f1648<\/p>\n<p style=\"padding-left: 30px\">SamAccountName&nbsp;&nbsp;&nbsp; : newtestuser71<\/p>\n<p style=\"padding-left: 30px\">SID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : S-1-5-21-1457956834-3844189528-3541350385-1441<\/p>\n<p style=\"padding-left: 30px\">Surname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : user71<\/p>\n<p style=\"padding-left: 30px\">UserPrincipalName :\nBW, that is all there is to using the <strong>Substring<\/strong><em> <\/em>method to obtain the first and last names for test users in&nbsp; 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&rsquo;t want to miss it.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create first and last names in Active Directory for test users. &nbsp;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&mdash;I [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,3,198,45],"class_list":["post-3217","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-users","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create first and last names in Active Directory for test users. &nbsp;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&mdash;I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3217"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3217\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}