Hello everyone,
I try to get with powershell mutli OUs to Export to a .CSV.
I have following Script:
$OUlist=@(‘OU=HAJ,OU=contoso users,DC=contoso,DC=de’,‘OU=ZHR,OU=contoso users,DC=contoso,DC=de’)
ForEach ($ou in $oulist) {Get-ADUser -Filter * -Searchbase $ou -Properties displayname, memberOf | select name, @{name=“MemberOf”;expression={$_.memberof -join “;”}} | export-csv e:\csv\bbbbbbbbbbbbb.csv}
The Export function is working, but i only get the second OU in the .CSV-Data (“ZHR”)
What is wrong?
Thank you
Kind Regards
TobiasHan
Olaf
#2
If you would have formatted your code as code and indented it nicely you might have been able to see what’s wrong a little easier.
$OUlist=‘OU=HAJ,OU=contoso users,DC=contoso,DC=de’,‘OU=ZHR,OU=contoso users,DC=contoso,DC=de’
ForEach ($ou in $oulist){
Get-ADUser -Filter * -Searchbase ou -Properties displayname, memberOf |
Select-Object name, @{name="MemberOf";expression={_.memberof -join “;”}} |
Export-Csv e:\csv\bbbbbbbbbbbbb.csv -Append
}
If you have the export inside the loop you overwright your csv file in every iteration of your loop!

That’s why you need
-Append
Hello Olaf,
perfect. Thank you. It works.
Kind Regards
Tobias