Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export-CSV exports length but not name

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" 
like image 537
Ninja Cowgirl Avatar asked Oct 18 '13 13:10

Ninja Cowgirl


People also ask

What is the difference between ConvertTo-CSV and Export-CSV commands?

Export-CSV is similar to ConvertTo-CSV , except that it saves the CSV strings to a file. The ConvertTo-CSV cmdlet has parameters to specify a delimiter other than a comma or use the current culture as the delimiter.

How do I remove double quotes from a CSV file in PowerShell?

Use the Foreach-Object cmdlet (% is an alias) to read each line as it comes from the file. Inside the script block for the Foreach-Object command, use the $_ automatic variable to reference the current line and the replace operator to replace a quotation mark with nothing.

How do I Export PowerShell to CSV?

To do this we can use the Export-CSV function in PowerShell. The Export-CSV function converts PowerShell objects into a CSV string and saves them into a CSV file. If you only need a CSV string, then you can also use the ConvertTo-CSV function in PowerShell.

How do I Export a csv file in ASP NET MVC?

Create a new MVC web project and name it "MVCImportExportCSV". Create a new "Controllers\HomeController. cs" file and add the following methods in it. // Copyright (c) Allow to distribute this code and utilize this code for personal or commercial purpose.


2 Answers

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.

like image 197
mjolinor Avatar answered Sep 28 '22 11:09

mjolinor


The previous answer does work, but what if someone was looking to output it into a CSV file.


This does NOT work:

$str_list = @('Mark','Henry','John') $str_list | Export-Csv .\ExportStrList.csv -NoType 

Because Export-Csv takes Objects and outputs properties. The only properties for a String[ ] is Length, so the CSV file only contains Lengths.

To fix this we need to change the String[ ] into an Object[ ]. The simplest way is with Select-Object.


Put each String into the Name property of a new Object[ ], like this:

$str_list = @('Mark','Henry','John') $obj_list = $str_list | Select-Object @{Name='Name';Expression={$_}} $obj_list | Export-Csv .\ExportStrList.csv -NoType 

Just to re-iterate, Select-Object outputs a custom PSObject that can easily be manipulated. This is very powerful information, use it wisely.

like image 35
Sean M. Avatar answered Sep 28 '22 09:09

Sean M.