Skip to main content

This function will remove the diacritics (accents) characters from a string.

function Remove-StringDiacritic
{
    <#
    .SYNOPSIS
        This function will remove the diacritics (accents) characters from a string.

    .DESCRIPTION
        This function will remove the diacritics (accents) characters from a string.

    .PARAMETER String
        Specifies the String(s) on which the diacritics need to be removed

    .PARAMETER NormalizationForm
        Specifies the normalization form to use
        https://msdn.microsoft.com/en-us/library/system.text.normalizationform(v=vs.110).aspx

    .EXAMPLE
        PS C:\> Remove-StringDiacritic "L'été de Raphaël"

        L'ete de Raphael

.NOTES
    Francois-Xavier Cat
    @lazywinadmin
    lazywinadmin.com
    github.com/lazywinadmin
#>
    [CmdletBinding()]
    param
    (
        [ValidateNotNullOrEmpty()]
        [Alias('Text')]
        [System.String[]]
        $String,

        [System.Text.NormalizationForm]
        $NormalizationForm = 'FormD'
    )

    foreach ($StringValue in $String)
    {
        Write-Verbose -Message "$StringValue"

        try
        {
            # Normalize the String
            $Normalized = $StringValue.Normalize($NormalizationForm)
            $NewString = New-Object -TypeName System.Text.StringBuilder

            # Convert the String to CharArray
            $normalized.ToCharArray() |
            ForEach-Object -Process {
                if ([Globalization.CharUnicodeInfo]::GetUnicodeCategory($psitem) -ne [Globalization.UnicodeCategory]::NonSpacingMark)
                {
                    [void]$NewString.Append($psitem)
                }
            }

            #Combine the new string chars
            Write-Output $($NewString -as [string])
        }
        Catch
        {
            Write-Error -Message $Error[0].Exception.Message
        }
    }
}