Pull Users from a list of OUs with powershell

Can someone help me modify this so instead of adding the OUs like below .. I can import them from a text file ?

$OUs = "CN=Users,DC=mylab,DC=com","OU=Admin,DC=mylab,DC=com"
$(ForEach ($OU in $OUs) {Get-aduser -filter * -SearchBase $OU -Properties SamAccountName,Mail | select SamAccountName,Mail }) | Export-csv C:\report.csv -nti

text File
CN=Users,DC=mylab,DC=com
CN=Users2,DC=mylab,DC=com
CN=Users3,DC=mylab,DC=com
CN=Users4,DC=mylab,DC=com
LVL 2
MilesLoganAsked:
Who is Participating?
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.

FOXActive Directory/Exchange EngineerCommented:
1. put just the usernames in a notepad list one below the other save as a .txt file
2. running powershell as an administrator type  

 import-module ActiveDirectory (press enter)

 Get-content -path c:\filename.txt | Get-ADuser | select samaccountname,mail >c:\report.csv

#where I have c:\filename.txt will be the path and the actual name of your .txt file
#after the > is where you want the file to reside after it is made
0
MilesLoganAuthor Commented:
no , the text file will include the OUs where I need to pull the users from ..
0
FOXActive Directory/Exchange EngineerCommented:
Are all the users part of the same domain?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

FOXActive Directory/Exchange EngineerCommented:
Miles,
If all the users are part of the same domain you do not need to specify the OU in the command. If you want to list the OU they are part of  in the csv just add distinguishedname with samaccountname and mail

 Get-content -path c:\filename.txt | Get-ADuser | select samaccountname,mail,distinguishedname >c:\report.csv
0
MilesLoganAuthor Commented:
They are part of the same domain , but I do not want to pull ALL the users from all OUs .. I am trying to just pull from a list of OUs
0
FOXActive Directory/Exchange EngineerCommented:
Understood

Get-Aduser -filter * -Searchbase  "OU=Users,DC=mylab,DC=com" | Get-ADuser | select samaccountname,mail  >c:\report.csv
0
FOXActive Directory/Exchange EngineerCommented:
revised.  In error I put get-aduser again...this is what you want

Get-Aduser -filter * -Searchbase  "OU=Users,DC=mylab,DC=com"  | select samaccountname,mail  >c:\report.csv
0
MilesLoganAuthor Commented:
Hi FoxLuv .. well .. the script below works for me but if I have 15 OUs that I want to pull the users from, I want to enter the OUs to a text file .. instead of $OUs = "CN=Users,DC=mylab,DC=com","OU=Admin,DC=mylab,DC=com" with 15 OUs

$OUs = "CN=Users,DC=mylab,DC=com","OU=Admin,DC=mylab,DC=com"
$(ForEach ($OU in $OUs) {Get-aduser -filter * -SearchBase $OU -Properties SamAccountName,Mail | select SamAccountName,Mail }) | Export-csv C:\report.csv -nti
0
FOXActive Directory/Exchange EngineerCommented:
Here is what you are trying to achieve
1. Put the path of your OUs (without quotation marks) ex. OU=Users,DC=mylab,DC=com
  line by line it a txt doc and save it as .csv file
2. running powershell in an elevated command prompt       import-module Activedirectory
 rename  the attached file ou.ps1 and save where you would like.

navigate to the path of the file and then .\ou.ps1  (press enter)
ou.txt
0
FOXActive Directory/Exchange EngineerCommented:
Miles,
Take off the last part of the script where I have >c:\report.csv  that is the wrong output command.  I will get back to you with the correct one
0
FOXActive Directory/Exchange EngineerCommented:
Appended attachment and instructions tested!!

1. Put the path of your OUs (without quotation marks) ex. OU=Users,DC=mylab,DC=com
   line by line it a txt doc and save it as .csv file
 2. running powershell in an elevated command prompt       import-module Activedirectory
  rename  the attached file ou.ps1 and save where you would like.

navigate to the path of the saved ou.ps1 file
run this command
.\ou.ps1 | out-file c:\temp\report.csv        (or wherever you choose to save)
ou.txt
0

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:
Thanks !! I was missing the "get-content"   I had it as $ou="c:\filewithous.csv"
0
FOXActive Directory/Exchange EngineerCommented:
Your welcome!!!
0
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.

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.