Change Attributes for users in OU but restrict only to parent OU

Stefanos Constantinou 61 Reputation points
2020-12-20T19:18:52.99+00:00

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?

Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2020-12-20T19:38:48.67+00:00

    Add -SearchScope OneLevel to the Get-ADUser.


  2. Thameur-BOURBITA 36,261 Reputation points Moderator
    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.

    get-aduser

    Please don't forget to mark this reply as answer if it help you to fix your issue


  3. Andreas Baumgarten 123.5K Reputation points MVP Volunteer Moderator
    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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.