Link to home
Start Free TrialLog in
Avatar of Ackles
AcklesFlag for Switzerland

asked on

Powershell replace german characters in string

Hello,
I have an issue with user name, it can contain these characters:
ä, à, ö, é, ü, è & all these characters cause issues because we want to create user names with these.
I am looking for a way to replace these characters to
ä = ae
ö = oe
ü = ue
é = e
é =e
à =a
Please note that these are in a viable $Name, the issue is that any name can have either 1 or more than 1 of these characters.
So the solution should be that it should look for all these characters & if they exist should be replaced.

Thanks a lot in advance for help.

Regards,
A
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

You need to perform multple replacements, like

$Name = $Name -replace "ä", "ae"
$Name = $Name -replace "ö", "ue"
:

Open in new window

Avatar of Ackles

ASKER

$Correct = (Get-Culture).TextInfo
$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $correct.ToTitleCase($FirstName.tolower())
$NameFirst = $FirstName -Replace ('ä', 'ae')
$NameFirst = $FirstName -Replace('ö', 'oe')


$LastName = Read-Host -Prompt 'LastName'
$LastName = $correct.ToTitleCase($LastName.tolower())
$DisplayName = $FirstName + " " + $LastName

$FirstName
$LastName
$UserName = ($NameFirst + "." + $LastName).ToLower() + "@test.com"
$UserName


This is my code & it doesn't work....
 $Correct = (Get-Culture).TextInfo
 $FirstName = Read-Host -Prompt 'FirstName'
 $FirstName = $correct.ToTitleCase($FirstName.tolower())

 $FirstName = $FirstName -Replace ("ä", "ae")
 $FirstName = $FirstName -Replace ("ö", "oe")


 $LastName = Read-Host -Prompt 'LastName'
 $LastName = $correct.ToTitleCase($LastName.tolower())
 $LastName = $LastName -Replace ("ä", "ae")
 $LastName = $LastName -Replace ("ö", "oe")

 $DisplayName = $FirstName + " " + $LastName

 $FirstName
 $LastName
 $UserName = ($FirstName + "." + $LastName).ToLower() + "@test.com"
 $UserName

Open in new window


Not elegant but works
Avatar of oBdA
oBdA

Note: this needs to be saved as Unicode, not ASCII or UTF8!
You might have to re-enter the special characters, don't know if they'll survive the transfer via Website.
$ReplaceMap = New-Object -TypeName System.Collections.Hashtable
$ReplaceMap['Ä'] = 'Ae'
$ReplaceMap['Ö'] = 'Oe'
$ReplaceMap['Ü'] = 'Ue'
$ReplaceMap['ä'] = 'ae'
$ReplaceMap['ö'] = 'oe'
$ReplaceMap['ü'] = 'ue'
$ReplaceMap['ß'] = 'ss'
$ReplaceMap['é'] = 'e'
$ReplaceMap['è'] = 'e'
$ReplaceMap['á'] = 'a'
$ReplaceMap['à'] = 'a'

$Correct = (Get-Culture).TextInfo
$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $Correct.ToTitleCase($FirstName.ToLower())
$ReplaceMap.Keys | % {$FirstName = $FirstName.Replace($_, $ReplaceMap[$_])}

$LastName = Read-Host -Prompt 'LastName'
$LastName = $Correct.ToTitleCase($LastName.ToLower())
$ReplaceMap.Keys | % {$LastName = $LastName.Replace($_, $ReplaceMap[$_])}

$DisplayName = $FirstName + " " + $LastName

$UserName = ($FirstName + "." + $LastName).ToLower() + "@test.com"
$FirstName
$LastName
$UserName

Open in new window

Main difference is the double quotes around the letters in the replace Statement

Here is a generic way to replace single characters: http://poshcode.org/1054

So I'd first replace the German Umlauts and ß with their corresponding double characters and send the rest through Remove-Diacritics
Avatar of Ackles

ASKER

Dear oBdA,
It does work, however the FirstName & LastName should be as entered by the user only the $UserName should be with replaced string.
I guess the replaced values have to be passed to a new variable for both & then those variables would create username....

Also, is it possible to remove any space in between e.g. the user inputs Jack Bauer but the result should be Jackbauer?

Thanks a lot!
A
Avatar of Ackles

ASKER

I passed it to variable & it works, however if the first character is the special character then it's skipped because it's capitalised.....


$ReplaceMap = New-Object -TypeName System.Collections.Hashtable
$ReplaceMap['Ä'] = 'Ae'
$ReplaceMap['Ö'] = 'Oe'
$ReplaceMap['Ü'] = 'Ue'
$ReplaceMap['ä'] = 'ae'
$ReplaceMap['ö'] = 'oe'
$ReplaceMap['ü'] = 'ue'
$ReplaceMap['ß'] = 'ss'
$ReplaceMap['é'] = 'e'
$ReplaceMap['è'] = 'e'
$ReplaceMap['á'] = 'a'
$ReplaceMap['à'] = 'a'

$Correct = (Get-Culture).TextInfo
$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $Correct.ToTitleCase($FirstName.ToLower())
$First = $FirstName
$ReplaceMap.Keys | % { $First = $First.Replace($_, $ReplaceMap[$_]) }

$LastName = Read-Host -Prompt 'LastName'
$LastName = $Correct.ToTitleCase($LastName.ToLower())
$Last = $LastName
$ReplaceMap.Keys | % { $Last = $Last.Replace($_, $ReplaceMap[$_]) }

$DisplayName = $FirstName + " " + $LastName

$UserName = ($First + "." + $Last).ToLower() + "@test.com"
$FirstName
$LastName
$First
$Last
$UserName
Avatar of Ackles

ASKER

I corrected it & is fine.
Can you please answer the removing of the space issue?
This will now replace only for the username, and remove the space (for the username only as well.
Works just fine here with a capital special character.
$ReplaceMap = New-Object -TypeName System.Collections.Hashtable
$ReplaceMap['Ä'] = 'Ae'
$ReplaceMap['Ö'] = 'Oe'
$ReplaceMap['Ü'] = 'Ue'
$ReplaceMap['ä'] = 'ae'
$ReplaceMap['ö'] = 'oe'
$ReplaceMap['ü'] = 'ue'
$ReplaceMap['ß'] = 'ss'
$ReplaceMap['é'] = 'e'
$ReplaceMap['è'] = 'e'
$ReplaceMap['á'] = 'a'
$ReplaceMap['à'] = 'a'

$Correct = (Get-Culture).TextInfo

$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $Correct.ToTitleCase($FirstName.ToLower())

$LastName = Read-Host -Prompt 'LastName'
$LastName = $Correct.ToTitleCase($LastName.ToLower())

$DisplayName = $FirstName + " " + $LastName

$UserFirst = $FirstName.Replace(' ', '')
$ReplaceMap.Keys | % {$UserFirst = $UserFirst.Replace($_, $ReplaceMap[$_])}
$UserLast = $LastName.Replace(' ', '')
$ReplaceMap.Keys | % {$UserLast = $UserLast.Replace($_, $ReplaceMap[$_])}

$UserName = ($UserFirst + "." + $UserLast).ToLower() + "@test.com"

$FirstName
$LastName
$UserName

Open in new window

Avatar of Ackles

ASKER

I added this to Replacemap:
$ReplaceMap = New-Object -TypeName System.Collections.Hashtable
$ReplaceMap['Ä'] = 'Ae'
$ReplaceMap['Ö'] = 'Oe'
$ReplaceMap['Ü'] = 'Ue'
$ReplaceMap['ä'] = 'ae'
$ReplaceMap['ö'] = 'oe'
$ReplaceMap['ü'] = 'ue'
$ReplaceMap['ß'] = 'ss'
$ReplaceMap['é'] = 'e'
$ReplaceMap['É'] = 'e'
$ReplaceMap['È'] = 'e'
$ReplaceMap['è'] = 'e'
$ReplaceMap['á'] = 'a'
$ReplaceMap['À'] = 'a'
$ReplaceMap['à'] = 'a'
$ReplaceMap['À'] = 'a'
$ReplaceMap[' '] = ''

& seems to work or is there another way?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ackles

ASKER

Awesome!!!