powershellpowershell-1.0

How to remove double quotes on specific column from CSV file using Powershell script


"ID","Full Name","Age"

"1","Jone Micale","25"

Here a sample from a CSV file that I created, and now I want to remove double quotes from only the ID and Age column value.

I tried different ways but I don't want to create a new file out of it. I just want to update the file with changes using PowerShell v1.


Solution

  • Export-Csv will always put all fields in double quotes, so you have to remove the undesired quotes the hard way. Something like this might work:

    $csv = 'C:\path\to\your.csv'
    (Get-Content $csv) -replace '^"(.*?)",(.*?),"(.*?)"$', '$1,$2,$3' |
        Set-Content $csv
    

    Regular expression breakdown: