find and replace special character " in multiple files using powershell

in particular path i need to find ""' and replace it with "' in multiple files

Tried below code but its not working due to special character to be found and replaced

$configFiles = Get-ChildItem . *.ini -rec
foreach ($file in $configFiles)
{
    (Get-Content $file.PSPath) |
    Foreach-Object { $_ -replace """'", ""'" } |
    Set-Content $file.PSPath
}

1 answer

  • answered 2018-01-11 21:05 TheIncorrigible1

    You're not escaping your characters properly:

    $configFiles = Get-ChildItem -Filter *.ini -Recurse
    ForEach ($file in $configFiles)
    {
        @(Get-Content -Path $file.FullName) -replace "`"{2}'", "`"'" |
            Set-Content -Path $file.FullName
    }