Get Users from multiple OUs with Poweshell

Hi EE .. I need to pull all users from multiple OUs and I can't figure it out .
Can someone help me modify this so I can enter all the OUs into one script ?

#$OU2 = "OU=Admin,DC=mylab,DC=com"

#This works for one OU but I can't figure out how to have it pull from other OUs.
$OU = "CN=Users,DC=mylab,DC=com"
Get-aduser -Filter * -SearchBase $OU | select SamAccountName
LVL 2
MilesLoganAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Schnell SolutionsSystems Infrastructure EngineerCommented:
As far as you cannot insert more than one OU in the Get-ADUser command, you rather create an array variable with all your OUs and then you make a loop in order to execute the command per OU.

Example:

$OUs = "OU=Admin,DC=mylab,DC=com", "CN=Users,DC=mylab,DC=com"

ForEach ($OU in $OUs) { Get-ADUser -Filter * -SearchBase $OU }

Enjoy :)
MilesLoganAuthor Commented:
HI schnellsolutions .. can you help with this error ? it outputs if I don't add the | .. but it errors if I try to select just one attribute.

$OUs = "CN=Users,DC=mylab,DC=com","OU=Admin,DC=mylab,DC=com"
ForEach ($OU in $OUs) {Get-aduser -Filter * -SearchBase $OU} | Select Name



At C:\PS\GetUsersFromOU.ps1:2 char:62
+ ForEach ($OU in $OUs) {Get-aduser -Filter * -SearchBase $OU} | Select Name
+                                                              ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement
Schnell SolutionsSystems Infrastructure EngineerCommented:
You are doing great. Just insert it inside the curly brackets:

ForEach ($OU in $OUs) { Get-ADUser -Filter * -SearchBase $OU | select name }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
That was it ! thank you so much schnellsolutions
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.