Multi OUs Export

Welcome Forums General PowerShell Q&A Multi OUs Export

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 7 months ago.

  • Author
    Posts
  • #75175

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 642
    Helping Hand
    Rank: Major Contributor

    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

      Participant
      Points: 0
      Rank: Member

      Hello Olaf,

      perfect. Thank you. It works.

      Kind Regards
      Tobias

The topic ‘Multi OUs Export’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort