Question

I have exported a CSV file from Power Shell with 3 columns. The first column I need to have double quotes but I do not want quotes in column 2 and 3. Is there a way to specify this when I export the csv file or is there some way I can modify the format to the csv file to remove the double quotes from column 2 and 3?

Current Format

"Column1","Column2","Column3",

Format Needed

"Column1",Column2,Column3,

Was it helpful?

Solution

This script should work for you:

############ Update these ############
$InputFile = 'C:\test\myfile.csv';
$OutputFile = 'c:\test\myfile.new.csv';
######################################

$Lines = Get-Content -Path $InputFile;

$NewLines = @()
foreach ($Line in $Lines) {
    $Match = [Regex]::Match($Line, '(.*?".*?")(.*)');
    $NewLines += $Match.Groups[1].Value + ($Match.Groups[2].Value -replace '"', '');
}

Set-Content -Path $OutputFile -Value $NewLines;

I tested with the following CSV file contents:

Col1,Col2,Col3
"asdf asdf","asdf","asdf"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top