Expanding system.object[] for export to csv

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 members list.   The output on the screen shows all the members but the csv file shows system.object[] if there is more than one member in the group.

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.   Can you look at the script below and tell me what I need to change to capture all the members?

$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

 

 

The screen shots below show the collection value prior to being exported to excel and the excel display.  

 

#1 - standard

 

 

 

#2 - using Out-string.trim()

 

 

All suggestions are greatly appreciated  [8-|]

 

 

 

  • When I want to dump a string array to a CSV I'll usually join them using whatever delimiter you want (in this case I used a carriage return):

     

    (get-distributiongroupmember $group.name | select-object -ExpandProperty name ) -join "`n"