Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-04
11
Medium Priority
?
825 Views
Last Modified: 2013-01-04
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?
0
Comment
Question by:MD187
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 41

Expert Comment

by:footech
ID: 38744660
import-module activedirectory
Get-ADUser -filter * | Where { $_.Name -match "TestOU" }

Open in new window

0
 
LVL 12

Expert Comment

by:ibrahim52
ID: 38744663
0
 
LVL 57

Assisted Solution

by:Mike Kline
Mike Kline earned 1000 total points
ID: 38744695
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:MD187
ID: 38744704
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
 
LVL 41

Expert Comment

by:footech
ID: 38744784
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
 

Author Comment

by:MD187
ID: 38744807
footech,

Correct. all users in any OU named "testOU"
0
 
LVL 57

Expert Comment

by:Mike Kline
ID: 38744831
Did you try the adfind command?

Thanks


Mike
0
 

Author Comment

by:MD187
ID: 38744845
Mike,

Worked like a charm!
0
 
LVL 41

Expert Comment

by:footech
ID: 38744849
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
 

Author Comment

by:MD187
ID: 38744918
Footech,

Can that be modifed to include child OU's?
0
 
LVL 41

Accepted Solution

by:
footech earned 1000 total points
ID: 38745060
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

Featured Post

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question