Export-CSV exports length but not name

Ninja Cowgirl picture Ninja Cowgirl · Oct 18, 2013 · Viewed 62.8k times · Source

I have this code that I am running from powershell. When I run it without the export-csv i get all the folder names on the screen.

dir | select -expand fullname | % { ($_ -split '\')[7] 

But if I add | export-csv c:\test.txt then I see following in the file not the folder name I expected just like I see it on the screen.

#TYPE System.String
"Length"
"13"
"18"
"20"
"22"
"29"
"21"
"24"
"11"
"17"
"20"
"20"

Answer

mjolinor picture mjolinor · Oct 18, 2013

Export-Csv exports a table of object properties and their values. Since your script is producing string objects, and the only property they have is length, that's what you got.

If you just want to save the list, use Out-File or Set-Content instead of Export-Csv.