Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 880
bruteForcePS Avatar asked May 04 '13 02:05

bruteForcePS


People also ask

How do you exclude an OU from ADUser?

To exclude specific sub OU's beneath Parent OU and get adusers from all other OU, we will have to use the Where-Object command to filter where the distinguishedname of the OU is not like the specific sub OU which we want to exclude.

How do you get a list of all users from a specific OU?

Simply open the “User Accounts” report, specify the path to the OU you're interested in and run the report. You'll get a list of the members of that OU with the following user account properties: name, logon name and status.

How do I add an ad to a specific OU?

You can create a Windows Active Directory (AD) user in a specific OU by using the -path parameter in 'New-ADuser' PowerShell command.


1 Answers

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

like image 102
serialhobbyist Avatar answered Sep 21 '22 08:09

serialhobbyist