Remove Quotes From CSV Files With PowerShell

📜Part of the PowerShell Complete Guide, every PowerShell post on this site, grouped by the job you are doing.

To remove quotes from a CSV file in PowerShell 7, import it and export it again with -UseQuotes AsNeeded. Values stay quoted only where they need it (a value with a comma inside, for example), so the file is still valid CSV. The common one-liner that strips every " character is quicker to type, but it breaks any value that has a comma in it, which I show below.

This post covers one file, a whole folder of them, and Windows PowerShell 5.1, which does not have -UseQuotes.

Create a Test File

A small file to try this on. The second row has a comma inside a value, which is the case that catches people out:

PowerShell
# Sample CSV with quotes around every value
@'
"first_name","surname","city","dob"
"pete","whyte","Glasgow","01/01/2000"
"joe","broke","Edinburgh, Leith","05/12/2001"
'@ | Set-Content .\TestData.csv

Get-Content .\TestData.csv
Creating a test CSV file with quoted values in PowerShell

Remove Quotes in PowerShell 7

PowerShell
# Change the file names to yours. AsNeeded keeps quotes only where a value needs them
Import-Csv .\TestData.csv | Export-Csv .\TestData_NoQuotes.csv -UseQuotes AsNeeded

Get-Content .\TestData_NoQuotes.csv

The output, from my PowerShell 7.7 test:

Code
first_name,surname,city,dob
pete,whyte,Glasgow,01/01/2000
joe,broke,"Edinburgh, Leith",05/12/2001

Every quote is gone except the pair around Edinburgh, Leith, which is the one that has to stay. -UseQuotes Never removes those too, but then the file has five values on a four-column row, which brings us to the trap.

Why Not Just Replace Every Quote?

The version this post originally showed, and the one most answers online give, replaces every " character:

PowerShell
# Strips every quote, including the ones a value needs
(Get-Content .\TestData.csv) -replace '"', '' | Set-Content .\TestData_NoQuotes.csv
The 2020 version: removing every quote from a CSV with -replace

On clean data it looks fine. On the test file, the joe row becomes joe,broke,Edinburgh, Leith,05/12/2001, and when anything reads it back the columns shift. Importing it again gave me city = Edinburgh and dob = Leith, with the real date lost. If you know your data has no commas inside values, the one-liner is fine. If you are not sure, use -UseQuotes AsNeeded.

Remove Quotes From Every CSV in a Folder

The same idea in a loop. Cleaned copies go to a separate folder, so the originals are untouched:

PowerShell
# Change these two folders to yours
$InputFolder  = 'C:\Data\CSV'
$OutputFolder = 'C:\Data\CSV-NoQuotes'

New-Item -ItemType Directory -Path $OutputFolder -Force | Out-Null

Get-ChildItem -Path $InputFolder -Filter *.csv | ForEach-Object {
    Import-Csv -Path $_.FullName |
        Export-Csv -Path (Join-Path $OutputFolder $_.Name) -UseQuotes AsNeeded
    "Cleaned $($_.Name)"
}

My 2024 version of this loop used the replace-every-quote method from the section above. On a demo folder with no commas inside values it gave the same result, quotes around every value going in and none coming out, and PowerShell 7 warns that -NoTypeInformation is obsolete:

Before and after: a CSV with quoted values and the cleaned copy with no quotes

The same file exported with -UseQuotes Never. It is safe here only because no value has a comma in it:

A single CSV file exported with Export-Csv -UseQuotes Never in PowerShell 7

Windows PowerShell 5.1

-UseQuotes arrived in PowerShell 7, so in Windows PowerShell 5.1 use a pattern that only removes the quotes around a whole value with no comma or quote inside it. A value like "say ""hi""" keeps its quotes, so nothing is lost. It gave the same output as AsNeeded in my 5.1 test:

PowerShell
# Windows PowerShell 5.1: change the file names to yours
Import-Csv .\TestData.csv |
    ConvertTo-Csv -NoTypeInformation |
    ForEach-Object { $_ -replace '(?<=^|,)"([^",]*)"(?=,|$)', '$1' } |
    Set-Content .\TestData_NoQuotes.csv

Keep -NoTypeInformation in 5.1: without it the first line of the file is a #TYPE line. In PowerShell 7 it is the default, and PowerShell 7 warns that the switch is obsolete if you pass it.

Not sure which one you are running? Check your PowerShell version with $PSVersionTable. If you are installing PowerShell 7 for this, here is how on Windows. More CSV jobs are in Export to CSV in PowerShell and Count Rows in CSV Files with PowerShell.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *