In previous tips we explained how you can escape entire string sequences. If you just need to escape an individual character, use HexEscape() like so:

PS> [Uri]::HexEscape('a')

This method actually retrieves the ASCII code and converts it to hexadecimal.

Actually, there is even the opposite route, and you can convert escaped characters back to normal. The ASCII code for “A” is 65, for example, which is 41 hexadecimal. The escaped representation of “A” is “%41” therefore, and this line would produce the “A”:

PS C:\> [Uri]::HexUnescape('%41',[ref]0)

(The second argument denotes the string position at which you want to convert the escaped character).

With this, you could now produce ranges of letters: first, produce the ASCII codes of the letters you need, and convert them manually in hexadecimal form. The -f operator can do this conversion for you:

PS> $decimal = 65
PS> $hex = '{0:X}' -f $decimal
PS> $hex 

Here are the escaped letters from A to Z:

65..90 | ForEach-Object { '%{0:X}' -f $_ }

Now you’d just need to unescape them:

65..90 | ForEach-Object { [Uri]::HexUnescape( ('%{0:X}' -f $_), [ref]0) } 

Don’t fall in love with this overkill, though. Type conversion can get you the same much easier:


Twitter This Tip! ReTweet this Tip!