Author Posts

July 18, 2017 at 8:10 am

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

July 18, 2017 at 8:26 am

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

July 18, 2017 at 11:05 am

Hello Olaf,

perfect. Thank you. It works.

Kind Regards
Tobias