question

ChristopherJack-1763 avatar image
0 Votes"
ChristopherJack-1763 asked ·

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

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
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

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 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Ian, That works a treat.. is there a reason why it it outputs it to a new line in the filer rather than as a row?

ORDERHEAD,
SO0029625
,
2021/17/22
,
,
,
DEFABUK,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
AMLY,
,
,
,
,
,
,
,
,

0 Votes 0 ·
RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered ·

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?

· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Rich, its a very strange CSV file template we have to add into our sales system, that is just the header line.

I am going to have to figure out how to add other lines.

The represent a column, but there are to be no headers within the file, they call it EDI?

0 Votes 0 ·

There are several versions of EDI (Electronic Data Interchange) file formats. Let's assume you're using one that's close to UN/EDIFACT. Each row (not column) represents an element. So you'll construct an array for every row, and every row may contain a different number of data. So using a CSV is not possible since a CSV contains a fixed number of columns in each row. To write the row, just use the "-join" operator with whatever separator you need and send the results to Out-File.

0 Votes 0 ·