Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConvertTo-Csv Output without quotes

Tags:

powershell

csv

I am using ConvertTo-Csv to get comma separated output

get-process | convertto-csv -NoTypeInformation -Delimiter ","

It outputs like:

"__NounName","Name","Handles","VM","WS",".....

However I would like to get output without quotes, like

__NounName,Name,Handles,VM,WS....
like image 285
Mohammad Nadeem Avatar asked Jun 06 '14 04:06

Mohammad Nadeem


People also ask

How do you remove quotes from CSV?

csv file. 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.

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 a quote from a string in PowerShell?

Trim() is actually the method intended for "trimming" characters from start/end of a String. It's still not perfect in this case, as there is also more suitable overloaded version Trim(Char[]). Simpler and will not remove escaped quotes from inside the string and possible intended whitespace. Check out my answer.


3 Answers

Here is a way to remove the quotes

get-process | convertto-csv -NoTypeInformation -Delimiter "," | % {$_ -replace '"',''}  

But it has a serious drawback if one of the item contains a " it will be removed !

like image 151
JPBlanc Avatar answered Sep 29 '22 07:09

JPBlanc


Hmm, I have Powershell 7 preview 1 on my mac, and Export-Csv has a -UseQuotes option that you can set to Never. :)

like image 39
js2010 Avatar answered Sep 29 '22 07:09

js2010


I was working on a table today and thought about this very question as I was previewing the CSV file in notepad and decided to see what others had come up with. It seems many have over-complicated the solution.
Here's a real simple way to remove the quote marks from a CSV file generated by the Export-Csv cmdlet in PowerShell.

Create a TEST.csv file with the following data.

"ID","Name","State"
"5","Stephanie","Arizona"
"4","Melanie","Oregon"
"2","Katie","Texas"
"8","Steve","Idaho"
"9","Dolly","Tennessee"

Save As: TEST.csv

Store file contents in a $Test variable
$Test = Get-Content .\TEST.csv

Load $Test variable to see results of the get-content cmdlet
$Test

Load $Test variable again and replace all ( "," ) with a comma, then trim start and end by removing each quote mark

$Test.Replace('","',",").TrimStart('"').TrimEnd('"')

Save/Replace TEST.csv file

$Test.Replace('","',",").TrimStart('"').TrimEnd('"') | Out-File .\TEST.csv -Force -Confirm:$false

Test new file Output with Import-Csv and Get-Content:

Import-Csv .\TEST.csv
Get-Content .\TEST.csv

To Sum it all up, the work can be done with 2 lines of code

$Test = Get-Content .\TEST.csv
$Test.Replace('","',",").TrimStart('"').TrimEnd('"') | Out-File .\TEST.csv -Force -Confirm:$false
like image 38
Fred Avatar answered Sep 29 '22 06:09

Fred