1

I have a CSV that I want to process in an array object and export as another CSV. However when I do that, instead of copying the values of the array, it copies the attribute of the object.

"Length","LongLength","Rank","SyncRoot","IsReadOnly","IsFixedSize","IsSynchronized","Count"
"52","52","1","System.Object[]","False","True","False","52"

How can resolve this?

EDIT:

The code that caused this:

Export-Csv $data test.csv

Where $data is the array that stores the output of the import-csv.

Mudassir
  • 725
  • 6
  • 28
  • 1
    Please show the code that produced this output as well as the corresponding sample input. Generally speaking, `Export-Csv` expects a list/array of objects as input and converts the properties of those objects to the fields of the CSV, taking the property names of the first object as the column header. – Ansgar Wiechers Nov 14 '19 at 10:55
  • 1
    `Export-Csv $data test.csv` -> `$data | Export-Csv test.csv` – Ansgar Wiechers Nov 14 '19 at 11:29
  • Thank you, this was very much helpful – Mudassir Nov 14 '19 at 11:43

1 Answers1

2

Apparently -InputObject became a positional parameter in PowerShell v6, otherwise the code you posted should've thrown an error

Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "test.csv" to type "System.Char". Error: "String must be exactly one character long."

Anyway, the code you posted is effectifly running

Export-Csv -InputObject $data -Path test.csv

However, the parameter -InputObject takes a single PSObject argument, not an array of PSObjects. If you pass it an array, the CSV output is created from that array object, not from the elements of the array.

Pipe the array to Export-Csv to have the output created from the array elements:

$data | Export-Csv -Path test.csv -NoType
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328