private/ConvertTo-EncodedString.ps1

function ConvertTo-EncodedString{

    [CmdletBinding()]
    param (
        [Parameter(Mandatory, Position = 0)]
        [AllowEmptyString()]
        [string] $Text,

        [Parameter(Mandatory, Position = 1)]
        [ValidateSet('Salesforce', 'Url', 'Xml', 'Hex')]
        [string] $EncodingStyle
    )

    $ErrorActionPreference = 'Stop'
    [bool] $IsDebug   = $DebugPreference   -ne 'SilentlyContinue'
    [bool] $IsVerbose = $VerbosePreference -ne 'SilentlyContinue'

    switch ($EncodingStyle) {

        'Salesforce'  {
            # This conversion is similiar to URL conversion with differences:
            # - hex value is in upper case
            # - not all characters are converted, e.g. space remains spaces and will not be converted to '+'
            $s = $Text
            $CharsToReplace = (
                '%',
                '&',
                '+',
                ',',
                '.',
                '/',
                '(',
                ')',
                ':',
                '>',
                '?', # added for record type picklist values
                '<'  # added for record type picklist values
            )
            foreach ($c in $CharsToReplace) {
                $s = $s.Replace($c, '%' + ([byte][char]$c).ToString('X')) # Example: Replace '&' with '%26'
            }
        }

        'Url'   {
            $s = [System.Web.HTTPUtility]::UrlEncode($Text)
        }

        'Xml'  {
            $s = $Text
            $s = $s.Replace("&", "&amp;")
            $s = $s.Replace("<", "&lt;")
            $s = $s.Replace(">", "&gt;")
            $s = $s.Replace("`'", "&apos;")
            $s = $s.Replace('"', "&quot;")
            #Write-Host $Text '|' $s
        }

        'Hex' {
            # This is the super-compact form of
            # - Convert incoming string to array of char
            # - loop through array of char and for each
            # - cast char to int -> ascii value
            # - cast int to hex representation of ascii value
            # - Concatenate resulting string array into one result string
            $s = $Text.ToCharArray() | ForEach-Object { ([int][char]$_).ToString('X') } | Join-String
        }

        Default {
            $s = $Text
        }
    }

    return $s
}