2

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"

plr108
  • 1,201
  • 11
  • 16
ragingdev
  • 33
  • 5
  • 1
    `"WürstWäßerWörfer" -creplace 'Ü','Ue' -creplace 'Ö','Oe' -creplace 'Ä','Ae' -creplace 'ü','ue' -creplace 'ö','oe' -creplace 'ä','ae' -replace 'ß','ss'` works for me just as expected. ;-) – Olaf Aug 03 '21 at 13:10
  • when I do it in the shell yes. I edited my script to `[CmdletBinding()] param ( [Parameter()] [string] $name ) return $name -creplace 'Ü','Ue' -creplace 'Ö','Oe' -creplace 'Ä','Ae' -creplace 'ü','ue' -creplace 'ö','oe' -creplace 'ä','ae' -replace 'ß','ss'` and it does not replace any chars when calling the script – ragingdev Aug 03 '21 at 13:24
  • 2
    Since the problem occurs with _string literals_ in your _source code_, the likeliest explanation is that your script file is misinterpreted by the Windows PowerShell engine, which happens if the script is saved as UTF-8 _without a BOM_. Try saving your script as UTF-8 _with BOM_; see [this answer](https://stackoverflow.com/a/54790355/45375) for more information. – mklement0 Aug 03 '21 at 14:03

2 Answers2

3

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.

Olaf
  • 4,690
  • 2
  • 15
  • 23
  • using it as a function was my first try. I used 'return' though. Maybe I should just reinstall all my PS-shells – ragingdev Aug 03 '21 at 13:36
  • Yep yours works in 5.1 thanks! – ragingdev Aug 03 '21 at 13:52
  • 3
    That's a nice wrapper, but note @ragingdev's original approach should work too, namely _if the source-code file's character encoding is properly recognized by the PowerShell engine_ - and it sounds like that was the true problem: a UTF-8-encoded script file _without a BOM_ misinterpreted by _Windows PowerShell_ (whereas PowerShell (Core) 7+ reads it correctly). By contrast, if your function definition is _pasted_ into an interactive session, the problem doesn't arise. – mklement0 Aug 03 '21 at 14:07
0

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

ragingdev
  • 33
  • 5
  • 4
    As noted in a comment on the question, the reasons this worked is that _PowerShell (Core) 7+_ correctly reads _BOM-less_ UTF-8 source-code files, whereas _Windows PowerShell_ (versions up to v5.1) _misinterprets_ them as ANSI-encoded. Therefore, the only thing you do need to do is to re-save your script file as UTF-8 _with BOM_. – mklement0 Aug 03 '21 at 14:14