Get-Aduser Effective Multi-OU Search

Hello,

Referring to the get-aduser searchbase parameter, is this just a single value parameter or is it usable for multi-OU searches for example:

Get-aduser filter * -searchbase OU=MyOU,DC=domain,DC=com OU#2, OU#3 -property WhenCreated,Name | FT | out-file dir


September 6th, 2013 7:52pm

Hello,

Referring to the get-aduser searchbase parameter, is this just a single value parameter or is it usable for multi-OU searches for example:

Get-aduser filter * -searchbase OU=MyOU,DC=domain,DC=com OU#2, OU#3 -property WhenCreated,Name | FT | out-file dir


When in doubt always refer to the source:
http://technet.microsoft.com/en-us/library/ee617241.aspx

You'll find the datatype expected by the parameter is String, not an array.  Your code above should error out right away.

In this case you'll need to make three calls to Get-ADUser and specify the different OUs each time.
  • Proposed as answer by Mike Laughlin Friday, September 06, 2013 7:59 PM
Free Windows Admin Tool Kit Click here and download it now
September 6th, 2013 7:56pm

Yes the errors made me wonder if I could use multiple strings on that, Ill look into another method, thanks.


September 6th, 2013 8:00pm

If you have an array of strings already made, you can just pipe it though a loop:

$ous = 'OU=Test 1,DC=domain,DC=com','OU=Test 2,DC=domain,DC=com'

$ous | ForEach { Get-ADUser -Filter * -SearchBase $_ }

Free Windows Admin Tool Kit Click here and download it now
September 6th, 2013 8:45pm

$UsersInOU1 = Get-aduser filter * -searchbase OU=MyOU,DC=domain,DC=com -property WhenCreated,Name | FT | out-file dir

$UsersInOU2 = Get-aduser filter * -searchbase OU=OU#2,DC=domain,DC=com -property WhenCreated,Name | FT | out-file dir

$UsersInOU3 = Get-aduser filter * -searchbase OU=OU#3,DC=domain,DC=com -property WhenCreated,Name | FT | out-file dir

$AllUsersList = $UsersInOU1 + $UsersInOU2 + $UsersInOU3

$AllUsersList | FT Name,WhenCreated

September 8th, 2015 8:11am

You realize this thread is over two years old and already answered, right?

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 8:13am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics