{"id":3048,"date":"2013-08-13T00:01:00","date_gmt":"2013-08-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/08\/13\/add-user-principal-names-in-active-directory-via-powershell\/"},"modified":"2013-08-13T00:01:00","modified_gmt":"2013-08-13T00:01:00","slug":"add-user-principal-names-in-active-directory-via-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/add-user-principal-names-in-active-directory-via-powershell\/","title":{"rendered":"Add User Principal Names in Active Directory via PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to add user principal names to users in Active Directory.<\/p>\n<p><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! We are planning for our Active Directory migration, and as part of that, I am reviewing users. The problem is that I found out that whoever set up our original installation did not assign values for user principal names (UPN). This will cause us a problem as we move to a federated environment. Can you offer an easy way to populate this value?<\/p>\n<p>&mdash;CG<\/p>\n<p><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 CG,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting on our <a href=\"http:\/\/en.wikipedia.org\/wiki\/Lanai_(architecture)\">lanai<\/a> and checking my <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a> email on my Microsoft Surface&nbsp;RT. I received an email from one of my friends in Hawaii. He was telling me about a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hukilau\" target=\"_blank\">Hukilau<\/a> he went to over the weekend. From his description, it makes me want to grab the Scripting Wife and head out west on the next available flight. The big problem right now, is the weather. I prefer August in Australia to August in Hawaii&mdash;it is really hot there.<\/p>\n<p>In Active Directory Users and Computers, the UPN shows up as the user logon name. It displays the UPN in two different fields, as shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3568.HSG-8-13-13-01.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3568.HSG-8-13-13-01.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>To find the actual Active Directory attribute name, I add a bunch of AAAs to the user logon name, and select a domain from the drop-down list. I then go into ADSI edit and look up the value. I see the following:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8726.HSG-8-13-13-02.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8726.HSG-8-13-13-02.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<h2>Searching for existing values<\/h2>\n<p>I use the <strong>Get-ADUser<\/strong> cmdlet to look for existing values for the <strong>UserPrincipalName<\/strong> attribute. To find the value of the <strong>UserPrincipalName<\/strong> attribute, I have to specify it for the <strong>&ndash;Properties<\/strong> parameter. I specify the <strong>SearchBase<\/strong> of the organizational unit (OU), and I use the <strong>*<\/strong> filter. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -Filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Properties userPrincipalName<\/p>\n<p>The command and associated output are represented in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7268.HSG-8-13-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7268.HSG-8-13-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Setting the UPN value<\/h2>\n<p>I use the <strong>Get-ADUser<\/strong> cmdlet to retrieve all the users to set. I pipe the resulting user objects to the <strong>Foreach-Object<\/strong> cmdlet, and in the script block, I use <strong>the Set-ADUser<\/strong> cmdlet. The <strong>Set-ADUser<\/strong> cmdlet has a <strong>&ndash;userPrincipalName<\/strong> parameter that makes it easy to set the UPN.<\/p>\n<p>To create the UPN, I use a hardcoded domain name, and I get the user&rsquo;s name from the <strong>Name<\/strong> attribute. I use parameter substitution and the <strong>&ndash;f <\/strong>format specifier to concatenate the user principal name. The command is shown here (this is a single-line command that I broke at the pipe for readability):<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -Filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Properties userPrincipalName | foreach { Set-ADUser $_ -UserPrincipalName (&#8220;{0}@{1}&#8221; -f $_.name,&#8221;iammred.net&#8221;)}<\/p>\n<p>CG, that is all there is to using Windows PowerShell to add the UPN for user accounts. Active Directory Week will continue tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I 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=\"mailto: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.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to add user principal names to users in Active Directory. &nbsp;Hey, Scripting Guy! We are planning for our Active Directory migration, and as part of that, I am reviewing users. The problem is that I found out that whoever set up our original [&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,20,45],"class_list":["post-3048","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-user-accounts","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to add user principal names to users in Active Directory. &nbsp;Hey, Scripting Guy! We are planning for our Active Directory migration, and as part of that, I am reviewing users. The problem is that I found out that whoever set up our original [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3048","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=3048"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3048\/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=3048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}