Powershell Active Directory - Limiting my get-adus

2020-07-03 04:03发布

问题:

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?

回答1:

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.).