Multi OUs Export

This topic contains 2 replies, has 2 voices, and was last updated by  TobiasHan 3 months ago.

  • Author
    Posts
  • #75175

    TobiasHan
    Participant

    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

  • #75178

    Olaf Soyk
    Participant

    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

      TobiasHan
      Participant

      Hello Olaf,

      perfect. Thank you. It works.

      Kind Regards
      Tobias

You must be logged in to reply to this topic.