Expanding System.Object for export to csv

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Juli Juli 4 months, 1 week ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #39319
    Profile photo of Juli
    Juli
    Participant

    I have a script that runs through a list of distribution groups and extracts the members and exports to a csv file.

    It works well except where there is more than one member in the group. The output on the screen shows all the members in the Members Column but the csv file shows system.object[] .

    I have tried using the output-string.trim() with limited success. This does remove the system.object[] but it only returns the first member in the list if there are more than one.

    Scenario #1 – No overloads

    On the console it looks like this:

    Scenario #2 using Output-string.Trim()

    Can you look at the script below and tell me what I need to change to capture all the members so they are correctly exported to the csv?

    $forwards = get-distributiongroup -filter {name -like "*forwarder"} | select-object name

    $forwarders = foreach($group in $forwards)

    {
    [pscustomobject]@{

    group = $group.name
    members = (get-distributiongroupmember $group.name | select-object name -ExpandProperty name | out-string).trim()

    }

    }

    $forwarders | export-csv c:\scripts\csvfiles\mail\forwarders.csv

    #39320
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Use a join to put all members into one string.

    (Get-ADGroupMember 'domain admins').name -join '; '

    #39325
    Profile photo of Juli
    Juli
    Participant

    Thanks Dan. That worked perfectly.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.