{"id":3057,"date":"2013-08-12T00:01:00","date_gmt":"2013-08-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/08\/12\/adding-office-locations-in-ad-ds-with-powershell\/"},"modified":"2013-08-12T00:01:00","modified_gmt":"2013-08-12T00:01:00","slug":"adding-office-locations-in-ad-ds-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/adding-office-locations-in-ad-ds-with-powershell\/","title":{"rendered":"Adding Office Locations in AD DS with PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to add office locations in Active Directory Domain Services.<\/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 in the midst of a domain migration at work, and I need to clean up a number of attributes in Active Directory prior to our migration. Part of our issue is that we have attributes that are missing values, and I just hate to migrate empty stuff. Is there anything you can do to help?<\/p>\n<p>&mdash;TV<\/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 TV,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I am listening to my customized Internet radio station with a cool app I found for Windows&nbsp;8 in the Windows Store. It is really cool to be able to select my listening preferences. I have actually spent a bit of time and created a couple of special Scripting Guy stations. It all depends on what I am writing. For example, Active Directory Domain Services questions simply beg for a bit of <a href=\"http:\/\/en.wikipedia.org\/wiki\/New_Orleans_jazz\" target=\"_blank\">classic New Orleans style jazz<\/a>&mdash;or, that is just me. Jazz also goes well with spearmint, wintergreen, peppermint, and lemon grass with a spoon full of Gun Powder Green tea. So I have my tunes and my tea. I guess I will answer your question.<\/p>\n<h2>First find the attribute<\/h2>\n<p>The first thing I need to do is to find the attribute that is missing. I first look at the user in Active Directory Users and Computers. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8372.HSG-8-12-13-01.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8372.HSG-8-12-13-01.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>It seems that the <strong>Office<\/strong> attribute is missing from all of the user objects. I add a bunch of AAAAAs to the Office field so I can find the attribute in ADSI Edit. I check with ADSI Edit, and sure enough the <strong>Office<\/strong> attribute is named <strong>physicalDeliveryOfficeName<\/strong> as shown in the following image. Good, that makes it easy.&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6740.HSG-8-12-13-02.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6740.HSG-8-12-13-02.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>I look around, and all of the cities are populated. So I check ADSI Edit, and I figure out that it uses a lower-case <strong>l<\/strong><em>&mdash;<\/em>as in <strong>L <\/strong>for Location. This is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5751.HSG-8-12-13-03.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5751.HSG-8-12-13-03.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<h2>Populating the Office field<\/h2>\n<p>In my example, the office names are the same as the city names. So all I need to do is to read all of the city names from all users in the TestOU organizational unit and add the city value to the <strong>Office<\/strong> attribute. First, let me make sure I can get the cities for all users. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -Filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Properties l<\/p>\n<p>The command and its output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5824.HSG-8-12-13-04.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5824.HSG-8-12-13-04.png\" alt=\"Image of command output\" width=\"550\" height=\"365\" \/><\/a><\/p>\n<p>As it turns out, I do not need to know the <strong>physicalDeliveryOfficeName<\/strong> attribute name because <strong>Set-ADUser<\/strong> has an <strong>&ndash;Office<\/strong> parameter. I compose the following command and use the <strong>&ndash;Whatif<\/strong> parameter to ensure that things will do what I want them to do.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-ADUser -Filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Properties l<\/p>\n<p style=\"padding-left: 30px\">| Foreach {Set-ADUser $_ -Office $_.l -whatif}<\/p>\n<p>The command and the output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0675.HSG-8-12-13-05.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0675.HSG-8-12-13-05.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>So, I remove the <strong>&ndash;Whatif<\/strong> and run the command again. This time, nothing returns from the command. (On my system, it takes about five seconds to run.) Here is the command (this is a one-line command broken 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 l|<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Foreach {Set-ADUser $_ -Office $_.l}<\/p>\n<p>I use Active Directory Users and Computers and check a few users. Here is the first user. The command worked.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5126.HSG-8-12-13-06.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5126.HSG-8-12-13-06.png\" alt=\"Image of menu\" width=\"400\" height=\"481\" \/><\/a><\/p>\n<p>TV, that is all there is to using Windows PowerShell to add office locations to users in a specific AD&nbsp;DS OU. Active Directory Domain Services Week will continue tomorrow when I will talk about Active Directrory migration cleanup.<\/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, talks about using Windows PowerShell to add office locations in Active Directory Domain Services. &nbsp;Hey, Scripting Guy! We are in the midst of a domain migration at work, and I need to clean up a number of attributes in Active Directory prior to our migration. Part of our issue [&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-3057","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, talks about using Windows PowerShell to add office locations in Active Directory Domain Services. &nbsp;Hey, Scripting Guy! We are in the midst of a domain migration at work, and I need to clean up a number of attributes in Active Directory prior to our migration. Part of our issue [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3057","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=3057"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3057\/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=3057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}