Link to home
Start Free TrialLog in
Avatar of MD187
MD187

asked on

Find all users in all OU's with a specific Name

Hey all,

I need to find all user objects in all OU's named "TestOU", Domain wide.  There are upward of 60 of these OU's.  

Any suggestions?
Avatar of footech
footech
Flag of United States of America image

import-module activedirectory
Get-ADUser -filter * | Where { $_.Name -match "TestOU" }

Open in new window

SOLUTION
Avatar of Mike Kline
Mike Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MD187
MD187

ASKER

Not so good with scripting, but learning quickly.  

To be clear,

import-module activedirectory
Get-ADUser -filter * | Where { $_.Name -match "SvcAcnts" }

Will get all the users in all the OU's named SvcAcnts, Domain wide?  The OU's are nested however at all different levels.
I think I may have misunderstood the intent of your question.  My script returns all user accounts named "TestOU" no matter what OU they are in.  But now I think you are asking to find all OU's named "TestOU", and then list all the users which are in those found OUs, correct?
Avatar of MD187

ASKER

footech,

Correct. all users in any OU named "testOU"
Did you try the adfind command?

Thanks


Mike
Avatar of MD187

ASKER

Mike,

Worked like a charm!
Import-Module ActiveDirectory
Get-ADOrganizationalUnit -filter {Name -like "TestOU"} | ForEach { Get-ADUser -filter * -searchBase $_ -searchScope onelevel }

Open in new window


This will return all users that are in any OU named "TestOU", and will not return any users that are in any child OUs of an OU named "TestOU".
Avatar of MD187

ASKER

Footech,

Can that be modifed to include child OU's?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial