{"id":4740,"date":"2012-11-02T00:01:00","date_gmt":"2012-11-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/11\/02\/use-powershell-to-update-address-info-in-active-directory\/"},"modified":"2012-11-02T00:01:00","modified_gmt":"2012-11-02T00:01:00","slug":"use-powershell-to-update-address-info-in-active-directory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-update-address-info-in-active-directory\/","title":{"rendered":"Use PowerShell to Update Address Info in Active Directory"},"content":{"rendered":"<p><b>Summary:<\/b> Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to update address information in Active Directory Domain Services (AD&nbsp;DS).\nMicrosoft Scripting Guy, Ed Wilson, is here. Yesterday, at the Charlotte Windows PowerShell user group meeting, one of the members was talking about adding mailing information to all of the users in a particular organizational unit. There are many reasons that this information may change: Street names are renamed all the time in the United States; cities incorporate other cities; postal codes change due to shifting populations. These are all reasons outside of one&rsquo;s control. And there are other reasons as well, for example, companies may consolidate offices, close offices, or move offices. Everyone who used to work in the Charlotte, North Carolina, office may suddenly find themselves commuting to Jacksonville, Florida.\nWhatever the reason is for updating address information, Windows PowerShell and the Active Directory module make this task fairly simple. It involves a number of attributes, however, and because all of the values for the attributes are static, this is a great time to use splatting.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b> &nbsp;&nbsp;I have <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=splatting&amp;sections=7618\" target=\"_blank\">several really good articles that talk about splatting<\/a>. I have written some of them, and others have been written by various guests. Splatting is a technique introduced with Windows PowerShell&nbsp;2.0, and, if you have not been using it, today is a great time to learn.<\/p>\n<h2>Populating address information in Active Directory<\/h2>\n<p>There are a couple of approaches to populating address information in Active Directory Domain Services (AD&nbsp;DS). The first approach is to search for particular values and modify the offending values. A second approach is to search for empty or null values for specific attributes, and then populate only the missing data. A third approach is to simply blast new values for a select group of users that meet a specific search criteria. This is the approach I take today.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b> &nbsp;&nbsp;This is the fourth in a series of articles about creating a test Active Directory environment. On Tuesday, I began the series with the <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\" target=\"_blank\">Create Test Users in a Test Active Directory Environment by Using PowerShell<\/a> Blog post, where I wrote a script to create a test organizational unit with 100 test user accounts. On Wednesday, I wrote <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/10\/31\/use-powershell-to-modify-existing-user-accounts-in-active-directory.aspx\" target=\"_blank\">Use PowerShell to Modify Existing User Accounts in Active Directory<\/a>, where I talked about using existing user information to create new information such as email addresses and personal web page URLs. On Thursday, I wrote <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/11\/01\/use-powershell-to-standardize-titles-in-active-directory.aspx\" target=\"_blank\">Use PowerShell to Standardize Titles in Active Directory<\/a><i><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/11\/01\/use-powershell-to-standardize-titles-in-active-directory.aspx\">.<\/a> <\/i>You should refer to those articles prior to reading today&rsquo;s article.\nThe first thing I need to do is to add <b>City<\/b><i> <\/i>attribute values to the user accounts in the TestOU organizational unit (OU). This is a simple matter, and I use the script shown here.<\/p>\n<p style=\"padding-left: 30px\"><b>SetADUserCityProperty.ps1<\/b><\/p>\n<p style=\"padding-left: 30px\">Import-Module ActiveDirectory<\/p>\n<p style=\"padding-left: 30px\">$city = &#8220;charlotte&#8221;, &#8220;atlanta&#8221;,&#8221;jacksonville&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter * |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Foreach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; $i= Get-Random -min 0 -Maximum 3<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Set-ADUser -identity $_ -City $city[$i] }\nTo ensure that I obtained a nice random mixture of cities in the OU, I use the following command.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Properties city -Filter * | group city -NoElement<\/p>\n<p style=\"padding-left: 30px\">Count Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; 25 jacksonville&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; 40 atlanta&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; 35 charlotte &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<h2>Create three hash tables for splatting<\/h2>\n<p>I want to populate the city, state, streetaddress, postalcode, and the country of each user in the TestOU. Because I know that the city properties are already populated, I know that I will be searching for the <b>City<\/b> property. However, it may be that the city will need to change once the new properties apply to the user (depends on the exact scenario). Therefore, I decide to create three hash tables. Each hash table resides in a variable with the name of the city. Each hash table is used to apply the appropriate attribute values when calling the <b>Set-ADUser<\/b> cmdlet from the Active Directory module.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp; &nbsp;Keep in mind, the key properties of the hash table (city, state, streetaddress, postalcode) must match exactly with the <b>Set-ADUser<\/b> cmdlet parameter names. This is important because ADSI stores the city information in an attribute named <b>l<\/b> (a lowercase letter <b>L<\/b>), but the <b>Set-ADUser<\/b> cmdlet uses the parameter name of <b>City<\/b><i> <\/i>to provide the same information. There are similar attribute name changes to be aware of when migrating old ADSI scripts to use the Active Directory cmdlets.\nThe three hash tables are shown here.<\/p>\n<p style=\"padding-left: 30px\">$charlotte = @{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;City&#8221; = &#8220;Charlotte&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;State&#8221; = &#8220;NC&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;StreetAddress&#8221; = &#8220;One Microsoft Way&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;PostalCode&#8221; = &#8220;12345&#8221; }<\/p>\n<p style=\"padding-left: 30px\">$atlanta = @{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;City&#8221; = &#8220;Atlanta&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;State&#8221; = &#8220;GA&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;StreetAddress&#8221; = &#8220;Two Microsoft Way&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;PostalCode&#8221; = &#8220;23456&#8221; }<\/p>\n<p style=\"padding-left: 30px\">$jacksonville = @{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;City&#8221; = &#8220;Jacksonville&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;State&#8221; = &#8220;FL&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;StreetAddress&#8221; = &#8220;Three Microsoft Way&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;PostalCode&#8221; = &#8220;34567&#8221; }\nThe code to make the change is simple. I use the <b>Get-ADUser<\/b> cmdlet to search the organizational unit, and I add a filter to look for the city equal to Charlotte. I then pipe the results to the <b>Set-ADUser<\/b> cmdlet and splat the Charlotte values. Because all three cities reside in the United States, I use a static <b>Country<\/b> value of <b>US<\/b>. Here is the code to assign the Charlotte values to users with a city value of Charlotte.<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;{city -eq &#8216;charlotte&#8217;} |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Set-ADUser @Charlotte -Country US\nNow, I do a cut-and-paste job to create two more searches to find Atlanta and to find Jacksonville. I also splat their values as well. Only the search filter and the splatted values change.<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;{city -eq &#8216;atlanta&#8217;} |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Set-ADUser @atlanta -Country US<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Filter `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;{city -eq &#8216;jacksonville&#8217;} |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Set-ADUser @jacksonville -Country US\nJoin me tomorrow for a guest blog post by the Scripting Wife as she talks about our upcoming European Scripting Tour.\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<b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to update address information in Active Directory Domain Services (AD&nbsp;DS). Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, at the Charlotte Windows PowerShell user group meeting, one of the members was talking about adding mailing information to all of the users in a particular [&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,4,170,20,45],"class_list":["post-4740","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-scripting-techniques","tag-splatting","tag-user-accounts","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to update address information in Active Directory Domain Services (AD&nbsp;DS). Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, at the Charlotte Windows PowerShell user group meeting, one of the members was talking about adding mailing information to all of the users in a particular [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4740","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=4740"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4740\/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=4740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}