Add -SearchScope OneLevel to the Get-ADUser.
Change Attributes for users in OU but restrict only to parent OU
I'm using the following command to replace the attributes of all AD users in the specified OU
Get-ADUser -SearchBase 'OU=Test OU,OU=Users and Computers,OU=Company HQ,DC=DOMAIN,DC=com' -filter * | Set-ADUser -Replace @{c="IT";co="Italy";countryCode="380"}
However, the command will change users' attributes which are in Sub-OUs
is there any parameter to restrict the command to change the attributes only in the specified OU?
3 answers
Sort by: Most helpful
-
-
Thameur-BOURBITA 35,436 Reputation points
2020-12-20T21:14:00.873+00:00 Hi,
You can add
-SearchScope Base
if you want modify only object in the parent OU.A SearchScope with a Base value searches only for the given user. If an OU is specified in the SearchBase parameter, no user will be returned by, for example, a specified Filter statement. A OneLevel query searches the immediate children of that path or object. This option only works when an OU is given as the SearchBase. If a user is given, no results are returned. A Subtree query searches the current path or object and all children of that path or object.
Please don't forget to mark this reply as answer if it help you to fix your issue
-
Andreas Baumgarten 117.5K Reputation points MVP
2020-12-21T18:55:44.237+00:00 There you go:
Get-ADUser -SearchBase 'OU=Test OU,OU=Users and Computers,OU=Company HQ,DC=DOMAIN,DC=com' -SearchScope OneLevel -filter * | Set-ADUser -Replace @{c="IT";co="Italy";countryCode="380"}
or
Get-ADUser -SearchBase 'OU=Test OU,OU=Users and Computers,OU=Company HQ,DC=DOMAIN,DC=com' -SearchScope Base -filter * | Set-ADUser -Replace @{c="IT";co="Italy";countryCode="380"}
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
Regards
Andreas Baumgarten