I would like to know how to Out-File String with Double Quotes into Single Column CSV by using PowerShell.
This is the csv File I am using with this header:
Name | output 1 | output 2 | output 3 |
Here is the PowerShell code I currently have:
$name = 'Peter Parker'
$out1 = 'testing'
$out2 = -join('"',"21,22,23", '"');
$out3 = 'output'
$text = "$name, $out1, $out2, $out3"
Write-Output $text
$text | Out-File .\test.csv -Encoding ascii -Append
Get-Content -Path .\test.csv
What I am looking for the result is like this:
Name | output 1 | output 2 | output 3 |
Peter Parker | testing | "21, 22, 23" | output |
But instead I got this result for the current code:
Name | output 1 | output 2 | output 3 |
Peter Parker | testing | "21 | 22 | 23" | output |
Is there anyway or work around to achieve my objective?
Thanks!
Why not letting Powershell do the work for you? Powershell is easily able to deal with structured data and csv files.
Example:
[PSCustomObject]@{
Name = 'Peter Parker'
out1 = 'testing'
out2 = ('"', "21,22,23", '"') -join ''
out3 = 'output'
} |
Export-Csv -Path .\test.csv -NoTypeInformation -Delimiter ','
Of course you can easily import this data again with:
Import-Csv -Path .\test.csv -Delimiter ','
and the result would be:
Name out1 out2 out3
---- ---- ---- ----
Peter Parker testing "21,22,23" output
You may keep in mind that Powershell is made for administrators - not software developers. It tries to make things easy for you. ;-)