{"id":3220,"date":"2013-07-18T00:01:00","date_gmt":"2013-07-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/07\/18\/use-powershell-to-set-ad-ds-users-display-names\/"},"modified":"2013-07-18T00:01:00","modified_gmt":"2013-07-18T00:01:00","slug":"use-powershell-to-set-ad-ds-users-display-names","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-set-ad-ds-users-display-names\/","title":{"rendered":"Use PowerShell to Set AD DS Users&#8217; Display Names"},"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 set the users&rsquo; display names in Active Directory Domain Services.<\/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 number of user names that were hastily created. I thought at first they were going to be temporary workers, but it seems that temporary is hanging around a lot longer than previously anticipated. This is actually causing issues with some of our Active Directory queries, because empty strings return when we were expecting data. I am wondering if there is an easy way to populate the display name&mdash; it would be fine to make it the same as the user name. I just need to get this done, and I do not want to spend a lot of time with it. Can you help me?\n&mdash;HH\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 HH,\nMicrosoft Scripting Guy, Ed Wilson, is here. This afternoon, it has not rained. In fact, it has not rained for two days. It must be some sort of record. Anyway, I am sipping a cup of Gun Powder Green Tea with a spoonful of lemon grass, a crushed cinnamon stick, a spoonful of rose hips, and a spoonful of Jasmine flowers. I let it steep for four minutes, and it is a lovely, light, and refreshing flavor. The tea is the perfect companion for doing a little bit of Active Directory Domain Services (AD DS) work.<\/p>\n<h2>Query AD&nbsp;DS<\/h2>\n<p>The first thing I always do is query AD DS to see what sort of data I have. This is easy to do when using Windows PowerShell and the Active Directory module. Here is the command I use to query for <strong>UserName<\/strong> and <strong>DisplayName<\/strong>:<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter * -Properties displayname | select name, displayname\n<span style=\"font-size: 12px\">The output is shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter * -Properties di<\/p>\n<p style=\"padding-left: 30px\">splayname | select name, displayname<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; displayname<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">newtestuser70<\/p>\n<p style=\"padding-left: 30px\">newtestuser71<\/p>\n<p style=\"padding-left: 30px\">newtestuser72<\/p>\n<p style=\"padding-left: 30px\">newtestuser73<\/p>\n<p style=\"padding-left: 30px\">&lt;truncated&gt;<\/p>\n<h2>Set DisplayName<\/h2>\n<p>To set the <strong>DisplayName<\/strong> attribute to be the same as the user name, I can pipe the results from the <strong>Get-ADUser<\/strong> cmdlet to the <strong>Set-ADUser<\/strong> cmdlet. The thing that is great about this is that the <strong>Set-ADUser<\/strong> cmdlet has a parameter for <strong>DisplayName<\/strong>, and this it makes it really easy. Here is the command I tried first:<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter * -Properties displayname | Set-ADUser -DisplayName $_.name\nBut this command does not work, because the value of <strong>$_<\/strong> is null here. Therefore, when I use the Up arrow to check my results, it comes up empty as shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter * -Properties di<\/p>\n<p style=\"padding-left: 30px\">splayname | select name, displayname<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; displayname<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">newtestuser70<\/p>\n<p style=\"padding-left: 30px\">newtestuser71<\/p>\n<p style=\"padding-left: 30px\">newtestuser72<\/p>\n<p style=\"padding-left: 30px\">&lt;truncated&gt;\nWell, I decide to try it without the <strong>$_ t<\/strong>o see if the cmdlet is smart enough to pick up the <strong>Name<\/strong> property directly without the assistance of <strong>$_<\/strong>. So here is the command I use:<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter * -Properties displayname | Set-ADUser -DisplayName name\nNow I check the results, and they are exactly what I was afraid of the first time&mdash;I have a bunch of names. See for yourself:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter * -Properties di<\/p>\n<p style=\"padding-left: 30px\">splayname | select name, displayname<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; displayname<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">newtestuser70&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name<\/p>\n<p style=\"padding-left: 30px\">newtestuser71&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name<\/p>\n<p style=\"padding-left: 30px\">newtestuser72&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name<\/p>\n<p style=\"padding-left: 30px\">newtestuser73&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name<\/p>\n<p style=\"padding-left: 30px\">&lt;truncated&gt;\nSo I try one more time. I assume that this command will work, but I was hoping that I could directly pipe to the <strong>Set-ADUser<\/strong> cmdlet without slowing things down with a <strong>Foreach-Object<\/strong> cmdlet (<strong>%<\/strong> is the alias). Here is the command:<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter * -Properties displayname | % {Set-ADUser -Identity $_ -DisplayName $_.name}\nAs I expected, the command worked properly. Here are the results:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter * -Properties di<\/p>\n<p style=\"padding-left: 30px\">splayname | select name, displayname<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; displayname<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">newtestuser70&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newtestuser70<\/p>\n<p style=\"padding-left: 30px\">newtestuser71&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newtestuser71<\/p>\n<p style=\"padding-left: 30px\">newtestuser72&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newtestuser72<\/p>\n<p style=\"padding-left: 30px\">newtestuser73&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newtestuser73<\/p>\n<p style=\"padding-left: 30px\">&lt;truncated&gt;\nSo all-in-all&hellip;not bad. It took me like less than three minutes to perform this task. It would have taken even less time if I went with what I suspected would have been the command in the first place. But hey, that is how one learns (or at least the way I learn&mdash;I just try it and see what happens).\nHH, that is all there is to using Windows PowerShell to set the display names of all your users in a particular organizational unit to be the same as the user names. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.\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><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to set the users&rsquo; display names in Active Directory Domain Services. &nbsp;Hey, Scripting Guy! I have a number of user names that were hastily created. I thought at first they were going to be temporary workers, but it seems that temporary is hanging [&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-3220","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 set the users&rsquo; display names in Active Directory Domain Services. &nbsp;Hey, Scripting Guy! I have a number of user names that were hastily created. I thought at first they were going to be temporary workers, but it seems that temporary is hanging [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3220","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=3220"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3220\/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=3220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}