TAGS :Viewed: 12 - Published at: a few seconds ago

[ get name of array instead of length powershell ]

Every time I try to export an string or an Array to an CSV, I get not the information from the array. I get the length of the string or of the Array.

For Example:

In Array there is a name called nbaps111.

When I do:

Export-CSV -Path "C:\Users\admrd\Desktop\test\bat\result_spwo_updated_on.csv" -NoTypeInformation -Delimiter ";"

I get in Excel 8 but not NBAPS111

What am I Doing Wrong? How can I convert it?

Answer 1


You can try this :

"azerty","toto" | % {New-Object -TypeName psobject -Property @{"name"=$_} } | Export-Csv C:\temp\t.csv -NoTypeInformation -Delimiter ";"

It gives :

cat C:\temp\t.csv
"name"
"azerty"
"toto"

Edited

The pipe part create a PsObject object with a property "name", then when you export this object the property apear as the column name. In the default behaviourthe property "length" of the type String appear in the CSV file.