How remove accents in PowerShell?
As per ip.'s answer, here is the Powershell version.
function Remove-Diacritics {param ([String]$src = [String]::Empty) $normalized = $src.Normalize( [Text.NormalizationForm]::FormD ) $sb = new-object Text.StringBuilder $normalized.ToCharArray() | % { if( [Globalization.CharUnicodeInfo]::GetUnicodeCategory($_) -ne [Globalization.UnicodeCategory]::NonSpacingMark) { [void]$sb.Append($_) } } $sb.ToString()}# Test data@("Rhône", "Basíl", "Åbo", "", "Gräsäntörmä") | % { Remove-Diacritics $_ }
Output:
RhoneBasilAboGrasantorma
Well I can help you with some of the code.....
I used this recently in a c# project to strip from email addresses:
static string RemoveDiacritics(string input) { string inputFormD = (input ?? string.Empty).Normalize(NormalizationForm.FormD); StringBuilder sb = new StringBuilder(); for (var i = 0; i < inputFormD.Length; i++) { UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(inputFormD[i]); if (uc != UnicodeCategory.NonSpacingMark) { sb.Append(inputFormD[i]); } } return (sb.ToString().Normalize(NormalizationForm.FormC)); }
I guess I can now say 'extending into a PowerShell script/form is left to the reader'.... hope it helps....
Another PowerShell translation of @ip for non C# coders ;o)
function Remove-Diacritics { param ([String]$sToModify = [String]::Empty) foreach ($s in $sToModify) # Param may be a string or a list of strings { if ($sToModify -eq $null) {return [string]::Empty} $sNormalized = $sToModify.Normalize("FormD") foreach ($c in [Char[]]$sNormalized) { $uCategory = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($c) if ($uCategory -ne "NonSpacingMark") {$res += $c} } return $res }}Clear-Host$name = "Un été de Raphaël"Write-Host (Remove-Diacritics $name )$test = ("äâûê", "éèà", "ùçä")$test | % {Remove-Diacritics $_}Remove-Diacritics $test