Multi OUs Export

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of TobiasHan TobiasHan 1 week, 3 days ago.

  • Author
  • #75175
    Profile photo of TobiasHan

    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

  • #75178
    Profile photo of Olaf Soyk
    Olaf Soyk

    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

    • #75185
      Profile photo of TobiasHan

      Hello Olaf,

      perfect. Thank you. It works.

      Kind Regards

You must be logged in to reply to this topic.