Replacing pesky 'umlaut' | standard way does not work

Viewed 336

I am trying to write a script, which I want to call to replace a German umlaut, ü,ä,ö etc.

I tried several versions using .replace and using -replace in one line and in multiple lines, it does not matter

Now I am stuck with this:

    [CmdletBinding()]
param (
    [Parameter()]
    [string]
    $name
)
$name = $name -replace 'ö','oe' 
$name = $name -replace 'ä','ae' 
$name = $name -replace 'ü','ue' 
$name = $name -replace 'ß','ss' 
$name = $name -replace 'Ö','Oe' 
$name = $name -replace 'Ü','Ue' 
$name = $name -replace 'Ä','Ae'
return $name

When I call the script/function it just does not replace any character. I tried "WürstWäßerWörfer" and it does nothing. If I set $name to "WürstWäßerWörfer" and only execute the $name =$name -replace commands it does work. If I call the script/function it doesn't work.

I call the script by .\entgermanisierung "WürstWäßerWörfer" and it returns "WürstWäßerWörfer"

2 Answers

You may turn your script into a function like this:

function Remove-Umlaut {
    param (
        [Parameter(Mandatory = $true)]
        [string]
        $String
    )
    $String -creplace 'Ü', 'Ue' -creplace 'Ö', 'Oe' -creplace 'Ä', 'Ae' -creplace 'ü', 'ue' -creplace 'ö', 'oe' -creplace 'ä', 'ae' -replace 'ß','ss'
}

Now you can call it just like any other function. For example:

$String = 'Das Üben von Xylophon und Querflöte ist zweckmäßig.'
Remove-Umlaut -String $String

And the result would be this:

Das Ueben von Xylophon und Querfloete ist zweckmaessig.

This code works in version 5.1 as well.

I changed the shell from PS 5.1 to 7.1.3. That did the trick.

Related