You can iterate each .csv
file from Get-ChildItem
and replace
the quotes "
with ''
using Set-Content
.
$files = Get-ChildItem -Path "YOUR_FOLDER_PATH" -Filter *.csv
foreach ($file in $files)
{
Set-Content -Path $file.FullName -Value ((Get-Content -Path $file.FullName -Raw) -replace '"', '')
}
Make sure to pass your folder path to -Path
, which tells Get-ChildItem
to fetch every file from this folder
Its also faster to use the -Raw
switch for Get-Content
, since it reads the file into one string and preserves newlines. If you omit this switch, Get-Content
will by default split the lines by newlines into an array of strings
If you want to read files in deeper sub directories as well, then add the -Recurse
switch to Get-ChildItem
:
$files = Get-ChildItem -Path "YOUR_FOLDER_PATH" -Filter *.csv -Recurse
Addtionally, you could also use Foreach-Object
here:
Get-ChildItem -Path "YOUR_FOLDER_PATH" -Filter *.csv -Recurse | ForEach-Object {
Set-Content -Path $_.FullName -Value ((Get-Content -Path $_.FullName -Raw) -replace '"', '')
}
Furthermore, you could replace Foreach-Object
with its alias %
. However, If your using VSCode and have PSScriptAnalyzer
enabled, you may get this warning:
'%' is an alias of 'ForEach-Object'. Alias can introduce possible problems and make scripts hard to maintain. Please consider changing alias to its full content.
Which warns against using aliases for maintainability. Its much safer and more portable to use the full version. I only use the aliases for quick command line usage, but when writing scripts I use the full versions.
Note: The above solutions could potentially corrupt the CSV if some lines need quoting. This solution simply goes through the whole file and replaces every quote with ''
. PowerShell 7 offers a -UseQuotes AsNeeded
option for Export-Csv
, so you may look into that instead.