{"id":3992,"date":"2013-03-21T00:01:00","date_gmt":"2013-03-21T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/03\/21\/use-the-powershell-ad-provider-to-modify-user-attributes\/"},"modified":"2013-03-21T00:01:00","modified_gmt":"2013-03-21T00:01:00","slug":"use-the-powershell-ad-provider-to-modify-user-attributes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-the-powershell-ad-provider-to-modify-user-attributes\/","title":{"rendered":"Use the PowerShell AD Provider to Modify User Attributes"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Active Directory module provider to modify user attributes in AD DS.<\/span><\/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! Just searching for users, or filtering for them, is not entirely all that useful. For me, I need to be able to make changes based on that search or filter. Is this easily done, or is it a real pain to do?<\/p>\n<p>&mdash;DR<\/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 DR,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, I received word that my latest appearance on Blain Barton&rsquo;s TechNet Radio Podcast is now live. In fact, Devon Musgrave at Microsoft Press wrote a cool blog about it. Blain and I talked about how certification literally changed our lives. The appearance was fun to do, and the discussion should be fun to listen to in addition to informational. <br \/> Check it out: <a href=\"http:\/\/blogs.msdn.com\/b\/microsoft_press\/archive\/2013\/03\/19\/author-news-ed-wilson-on-how-certification-changed-his-life.aspx\" target=\"_blank\">Author news: Ed Wilson on how certification changed his life<\/a>.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;This is the fourth blog in a series about using the Active Directory module.<\/p>\n<ul>\n<li>In <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/03\/18\/playing-with-the-ad-drive-for-fun-and-profit.aspx\" target=\"_blank\">Playing with the AD: Drive for Fun and Profit<\/a>, I provided an overview about the AD: drive and the Active Directory module.<\/li>\n<li>In <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/03\/19\/find-active-directory-user-information-with-the-windows-powershell-provider.aspx\" target=\"_blank\">Find Active Directory User Information with the PowerShell Provider<\/a><em>, <\/em>I talked about how to use the Windows PowerShell provider to find user information in Active Directory.<\/li>\n<li>In <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/03\/20\/use-powershell-to-find-non-default-user-properties-in-ad.aspx\" target=\"_blank\">Use PowerShell to Find Non-Default User Properties in AD<\/a>, I talked about how to retrieve additional properties beyond the four basic ones.<\/li>\n<\/ul>\n<h2>Find users that need modification<\/h2>\n<p>Suppose there are several users in an OU that have a specific attribute that needs to be changed. This may because of merging departments or in the case of a corporate buyout. In the following image, the office location of the user needs to be changed.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5810.hsg-3-21-13-01.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5810.hsg-3-21-13-01.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>This particular task begins with finding the user, and it can be a bit frustrating. After attempting to find a user object with an office in Raleigh that resides in the Charlotte OU, I was about to give up. Then I decided to look up the user in ADSI Edit. Here is what I found out.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5305.hsg-3-21-13-02.png\"><img decoding=\"async\" title=\"Image of results\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5305.hsg-3-21-13-02.png\" alt=\"Image of results\" \/><\/a><\/p>\n<p>The property that appears as <strong>Office<\/strong><em> <\/em>in ADUC is called <strong>PhysicalDeliveryOfficeName<\/strong><em>. <\/em>Without a quick peek at ADSI Edit, I would never have discovered that one! So now, all is groovy.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;I am using a PS Drive called Charlotte that is homed on the Charlotte OU. I explained how to create it in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/03\/18\/playing-with-the-ad-drive-for-fun-and-profit.aspx\">Playing with the AD: Drive for Fun and Profit<\/a>.<\/p>\n<p>Keep in mind that I do not use spaces with this filtering technique. Although it does not generate an error, the following command does not find any users either.<\/p>\n<p style=\"padding-left: 30px\">PS charlotte:\\&gt; Get-Item -Filter &#8220;physicaldeliveryofficename = Raleigh&#8221; -Path *<\/p>\n<p style=\"padding-left: 30px\">PS charlotte:\\&gt;<\/p>\n<p>So I need to remove the spaces. The following command finds the two users I need to modify. The cool thing about using <strong>Get-Item<\/strong> here is that if more than one item matches the filter, it returns all matching objects. Here is the command I use.<\/p>\n<p style=\"padding-left: 30px\">PS charlotte:\\&gt; Get-Item -Filter &#8220;physicaldeliveryofficename=Raleigh&#8221; -Path *<\/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;ObjectClass&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DistinguishedName<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">Regular User&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; user&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CN=Regular User,ou=charlotte,dc=iammred&#8230;<\/p>\n<p style=\"padding-left: 30px\">Sample User&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; user&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CN=Sample User,ou=charlotte,dc=iammred,&#8230;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">PS charlotte:\\&gt;<\/p>\n<h2 style=\"padding-left: 30px\"><span style=\"font-size: 1.5em\">Modify the user object with Set-ItemProperty<\/span><\/h2>\n<p>Now that I know what filter I need to use and I have verified that I can find the users that need updating, it is time to perform the actual modification. The cool thing here is that I use the <strong>Set-ItemProperty<\/strong> cmdlet to make the modifications. By using <strong>Set-ItemProperty<\/strong>, it becomes really easy to modify the users whose office changed from Raleigh to Charlotte. In fact, the command is so easy to use that it contains its own <strong>Filter<\/strong><em> <\/em>parameter.<\/p>\n<p>Therefore, all I need to do is to copy the filter I used with the <strong>Get-Item<\/strong> cmdlet and paste it into the <strong>Filter<\/strong><em> <\/em>parameter. Because I am potentially going to make a bunch of changes, I decided to use the <strong>WhatIf<\/strong><em> <\/em>parameter first to tell me exactly what the command will do. The following command illustrates using <strong>WhatIf<\/strong><em> <\/em>and the <strong>Filter<\/strong><em> <\/em>parameters with the <strong>Set-ItemProperty<\/strong> cmdlet to model a potential change to user objects in AD DS. The output generated by the <strong>WhatIf<\/strong><em> <\/em>parameter lets me know the two users who will receive the modification.<\/p>\n<p style=\"padding-left: 30px\">PS charlotte:\\&gt; Set-ItemProperty -Filter &#8220;physicaldeliveryofficename=Raleigh&#8221; -Path *<\/p>\n<p style=\"padding-left: 30px\">&nbsp;-Name physicaldeliveryofficename -Value Charlotte -WhatIf<\/p>\n<p style=\"padding-left: 30px\">What if: Performing operation &#8220;Set&#8221; on Target &#8220;CN=Regular User,ou=charlotte,dc=iammred,dc=net&#8221;.<\/p>\n<p style=\"padding-left: 30px\">What if: Performing operation &#8220;Set&#8221; on Target &#8220;CN=Sample User,ou=charlotte,dc=iammred,dc=net&#8221;.<\/p>\n<p style=\"padding-left: 30px\">PS charlotte:\\&gt;<\/p>\n<p>Now that I see the two users who will receive the changed office locations are the same two users I found by using the <strong>Get-Item<\/strong> cmdlet, I can be reasonably certain that the command will perform what I want it to do. Therefore, I use the up arrow to retrieve my previous command, and this time I erase the <strong>&ndash;WhatIf<\/strong><em> <\/em>portion of the command. (I always use <strong>WhatIf<\/strong><em> <\/em>at the end of a cmdlet call so that it is easy to delete when it comes time to run the actual command.) As shown here, nothing returns from running the command.<\/p>\n<p style=\"padding-left: 30px\">PS charlotte:\\&gt; Set-ItemProperty -Filter &#8220;physicaldeliveryofficename=Raleigh&#8221; -Path *<\/p>\n<p style=\"padding-left: 30px\">&nbsp;-Name physicaldeliveryofficename -Value Charlotte<\/p>\n<p style=\"padding-left: 30px\">PS charlotte:\\&gt;<\/p>\n<p>I use the up arrow a couple of times and retrieve my previous <strong>Get-Item<\/strong> command to see if any users still have offices in Raleigh. As shown here, the command finds no users&mdash;they have all been changed.<\/p>\n<p style=\"padding-left: 30px\">PS charlotte:\\&gt; Get-Item -Filter &#8220;physicaldeliveryofficename=Raleigh&#8221; -Path *<\/p>\n<p style=\"padding-left: 30px\">PS charlotte:\\&gt;<\/p>\n<p>OK. One more check&hellip;seeing is believing. Let me verify that the office is changed on one of the users I found earlier. Yep, the user office location is in fact changed from Raleigh, as shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8055.hsg-3-21-13-03.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8055.hsg-3-21-13-03.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>DR, that is all there is to using modifying user objects with the Windows PowerShell Active Directory provider. Active Directory Week will continue tomorrow when I will talk about further use of Windows PowerShell techniques with the Active Directory module provider.<\/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 the Windows PowerShell Active Directory module provider to modify user attributes in AD DS. &nbsp;Hey, Scripting Guy! Just searching for users, or filtering for them, is not entirely all that useful. For me, I need to be able to make changes based on that search or [&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,414,3,4,303,20,45],"class_list":["post-3992","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-providers","tag-scripting-guy","tag-scripting-techniques","tag-searching","tag-user-accounts","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Active Directory module provider to modify user attributes in AD DS. &nbsp;Hey, Scripting Guy! Just searching for users, or filtering for them, is not entirely all that useful. For me, I need to be able to make changes based on that search or [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3992","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=3992"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3992\/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=3992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}