Removing Quotes from all CSV Files in a Directory

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

Managing CSV files often involves cleaning up data before processing. A common task is removing unwanted quotation marks from the contents of CSV files.

I wrote a blog post on how to remove quotes from a single CSV file using PowerShell some years ago, which I have just updated. At the end, I had the idea of adding a follow-up which is to do the same, remove quotes in a CSV file, but for multiple files.

Ideally you should be cleaning the data at source rather than performing this task. There’s many ways to do this too, I think you should use the one you find the easiest that saves most of your time.

PowerShell Script to Remove Quotes from Multiple CSV Files

Here is a script to remove quotes from all CSV files in a specified directory and save the cleaned versions in a new output directory:

PowerShell
# Define the input directory and output directory
$InputDirectory = "C:\Path\To\Input\CSVFiles"
$OutputDirectory = "C:\Path\To\Output\CleanedCSVFiles"

# Create the output directory if it doesn't exist
if (-not (Test-Path $OutputDirectory)) {
    New-Item -ItemType Directory -Path $OutputDirectory
}

# Get all CSV files in the input directory
$CSVFiles = Get-ChildItem -Path $InputDirectory -Filter "*.csv"

foreach ($File in $CSVFiles) {
    # Read the content of the current CSV file, remove quotes, and save it to a new file
    $InputFilePath = $File.FullName
    $OutputFilePath = Join-Path -Path $OutputDirectory -ChildPath $File.Name

    # Process the file: Remove quotes and output to the new file
    Import-Csv -Path $InputFilePath |
    ConvertTo-Csv -NoTypeInformation |
    ForEach-Object { $_ -Replace '"', "" } |
    Out-File -FilePath $OutputFilePath -Force -Encoding ascii

    Write-Host "Processed file: $InputFilePath -> $OutputFilePath"
}

Write-Host "All CSV files have been processed and saved in $OutputDirectory."

Here is the same loop run against a demo folder in PowerShell 7. The input file has quotes around every value and the output file has none:

PowerShell 7: the quote-removal loop run on a demo folder prints the NoTypeInformation obsolete warning, then Get-Content shows the input file with "Db","SizeMB", "master","6", "msdb","22" and the output file as Db,SizeMB, master,6, msdb,22

The yellow warnings are PowerShell 7 saying -NoTypeInformation is obsolete. They are harmless: PowerShell 7 leaves the type line out by default, so the parameter does nothing there. Keep it if the script also runs in Windows PowerShell 5.1, which adds a #TYPE line to the top of the output without it.

How it works:
– Define Directories:
The script processes files from the InputDirectory and saves cleaned versions in the OutputDirectory. Ensure you update these paths with your actual directories.
– Batch Processing: Uses Get-ChildItem to loop through all .csv files in the input directory.
– Remove Quotes: Reads each file’s content, removes quotes, and writes the cleaned data to a new file.
– Output Directory: Files are saved with their original names in the designated output directory to avoid overwriting the source files.

Recommendations:
– Monitor Performance: If you’re working with large CSV files, monitor your system resources during processing.
– Backup Original Files: Always create backups of the original files before running bulk operations.
– Validate Data: Check the output files to ensure the data integrity is maintained after quote removal.

PowerShell 7: Let Export-Csv Drop the Quotes

In PowerShell 7, Export-Csv and ConvertTo-Csv have a -UseQuotes parameter, so the find and replace step is not needed. Inside the loop, this one line does the same job as the four-line pipeline:

PowerShell
# PowerShell 7 and later
Import-Csv -Path $InputFilePath | Export-Csv -Path $OutputFilePath -UseQuotes Never
PowerShell 7: Import-Csv piped to Export-Csv -UseQuotes Never, then Get-Content shows the new file as Db,SizeMB, master,6 and msdb,22 with no quotes

-UseQuotes AsNeeded is the safer choice when a value can contain a comma. It only quotes the fields that need it, so a value of 1,5 is written as "1,5" and the row keeps its two columns. Stripping every quote, as the script above does, splits that value in two. -UseQuotes does not exist in Windows PowerShell 5.1, where the script above is still the way to do it.

If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

Leave a Reply

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