[powershell] Powershell Active Directory - Limiting my get-aduser search to a specific OU [and sub OUs]

Just wrote a script that disables an account, moves it to a disabled OU and changes the description on the user object, but I want to make it more efficient.

My work AD structure has all users under Root - accounts OU, and the 50 or so department OUs under that accounts OU.

How can I START my search at the accounts OU and have it check every sub OU in accounts?

This question is related to powershell active-directory

The answer is


If I understand you correctly, you need to use -SearchBase:

Get-ADUser -SearchBase "OU=Accounts,OU=RootOU,DC=ChildDomain,DC=RootDomain,DC=com" -Filter *

Note that Get-ADUser defaults to using

 -SearchScope Subtree

so you don't need to specify it. It's this that gives you all sub-OUs (and sub-sub-OUs, etc.).