Expanding Objects in Export-CSV

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Greg Tate Greg Tate 5 months, 2 weeks ago.

  • Author
    Posts
  • #43566
    Profile photo of Greg Tate
    Greg Tate
    Participant

    When I export to csv and open in Excel, certain columns indicate an array of objects, such as System.String[].
    But when I pipe to format-list these fields show their normal values as expected.

    What tricks can I use to get my columns to display properly?

    As an example, I am using the get-messagetracking cmdlet and doing a simple pipe to export-csv.

    Thanks in advance!

  • #43578
    Profile photo of random commandline
    random commandline
    Participant

    Use the join operator on the property. (-join ',' or -join "`n")

    # Example
    $nums = 'one','two','three'
    [PSCustomObject]@{
        'one' = 'one'
        'nums' = $nums -join ','
    }  | Export-Csv .\test.csv
    
  • #43602
    Profile photo of Greg Tate
    Greg Tate
    Participant

    Thanks for the tip!
    This help me get to the info I needed.

    The end result of my command looks something like:

    get-messagetrackinglog -start 6/1/16 -sender sender@company.com | 
    select *, 
    @{ n='recipients2'; e={$_.recipients -join ','}},
    @{ n='recipientstatus2'; e={$_.recipientstatus -join ','}},
    @{ n='eventdata2'; e={$_.eventdata -join ','}} |
    export-csv -NoTypeInformation -Path export.csv -delimiter ';'
    

    There were three fields returning arrays of strings, so I created new fields for them using the join operator.

    • This reply was modified 5 months, 2 weeks ago by Profile photo of Greg Tate Greg Tate. Reason: formatting

You must be logged in to reply to this topic.