Export to CSV outputs the deinfitions/lengths rather than the values

Christopher Jack 1,611 Reputation points
2021-03-22T11:51:55.523+00:00

Hi,

When I am trying to output to a CSV or txt file

$header = @();
$header += "ORDERHEAD", $A[3], $today, "", "", "DEFABUK", "", "", "", "", "",  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "AMLY", "", "", "", "", "", "", "", ""

$header | Export-Csv -Path c:\test\test.csv -Delimiter ',' -NoTypeInformation

It outputs

"Length"
"9"
"9"
"10"
"0"
"0"
"7"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"4"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"

So it is missing the first string and then just outputting attributes any help appreciated

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,359 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,491 Reputation points Microsoft Vendor
    2021-03-22T12:52:17.817+00:00

    Hi,

    The export-csv cmdlet exports the properties of objects to the csv file. To output the string you can use out-file

    $header | Out-File c:\test\test.txt  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-03-22T14:58:27.537+00:00

    If you're satisfied with a string then this should get you the data in the format you expect:

    $header = @();
    $header += "ORDERHEAD", $A[3], $today, "", "", "DEFABUK", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "AMLY", "", "", "", "", "", "", "", ""
    $header -join "," | Out-File c:\test\test.TXT
    

    A CSV file requires a bit more work, but it isn't evident in your question what the names of the column's are (if there are any), and what the data in the columns are. For example there are 36 elements in the "$header" array. Do all of them represent data, or are some the names of a column?

    1 person found this answer helpful.