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

Christopher Jack 1,616 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 for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    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 47,901 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.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.