MilesLogan
asked on
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
$OUs = "CN=Users,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
ASKER
no , the text file will include the OUs where I need to pull the users from ..
Are all the users part of the same domain?
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,distin guishednam e >c:\report.csv
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,distin
ASKER
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
Understood
Get-Aduser -filter * -Searchbase "OU=Users,DC=mylab,DC=com" | Get-ADuser | select samaccountname,mail >c:\report.csv
Get-Aduser -filter * -Searchbase "OU=Users,DC=mylab,DC=com"
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
Get-Aduser -filter * -Searchbase "OU=Users,DC=mylab,DC=com"
ASKER
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
$OUs = "CN=Users,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
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
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
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks !! I was missing the "get-content" I had it as $ou="c:\filewithous.csv"
Your welcome!!!
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