Manoj Bansal
Manoj Bansal

Reputation: 31

Powershell replace special characters like ü

(Get-Content -Path $filePath) -creplace ${find}, $replace | Add-Content -Path $tempFilePath 

If $find and $replace contains below values it's not replacing it

ç c
à a
é e

Please help

Upvotes: 2

Views: 2585

Answers (2)

Theo
Theo

Reputation: 61168

If by characters like ü you mean Diacritics you can use this:

function Replace-Diacritics {
    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string] $Text
    )
    ($Text.Normalize([Text.NormalizationForm]::FormD).ToCharArray() | 
     Where-Object {[Globalization.CharUnicodeInfo]::GetUnicodeCategory($_) -ne 
                   [Globalization.UnicodeCategory]::NonSpacingMark }) -join ''
}

# you can experiment with the `-Encoding` parameter
Get-Content -Path 'D:\Test\TheFile.txt' -Encoding UTF8 -Raw | Replace-Diacritics | Set-Content -Path 'D:\Test\TheNewFile.txt'

Upvotes: 2

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41695

You need to -Encoding UTF8 to the Get-Content method, for reading the special characters correctly:

(Get-Content -Path $filePath -Encoding UTF8) -creplace ${find}, $replace | Add-Content -Path $tempFilePath 

Upvotes: 2

Related Questions