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?
MD187Asked:
Who is Participating?
 
footechConnect With a Mentor Commented:
Yes, simply by modifying the -searchScope parameter to be "subtree" (or you can remove it entirely, as "subtree" is the default).
Import-Module ActiveDirectory
Get-ADOrganizationalUnit -filter {Name -like "TestOU"} | ForEach { Get-ADUser -filter * -searchBase $_ -searchScope subtree }

Open in new window

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

Open in new window

0
 
ibrahim52Team LeaderCommented:
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
Mike KlineConnect With a Mentor Commented:
There is no way to filter for an OU in an LDAP filter, but that is where a tool like adfind by MVP Joe Richards comes into play

http://www.joeware.net/freetools/tools/adfind/index.htm

adfind -default -incldn TestOU -f "&(objectcategory=person)(objectclass=user)" samaccountname

The -incldn is the key that adfind can provide.  You can also exclude DNs if you want to go down that route.

I created two OUs "TestOU2" and TestOU3 and tested the query above and it found all 10 users I moved to those OUs.

Thanks

Mike
0
 
MD187Author Commented:
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.
0
 
footechCommented:
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?
0
 
MD187Author Commented:
footech,

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

Thanks


Mike
0
 
MD187Author Commented:
Mike,

Worked like a charm!
0
 
footechCommented:
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".
0
 
MD187Author Commented:
Footech,

Can that be modifed to include child OU's?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.