powershellunicodestring-literalsunicode-literals

How do I encode Unicode character codes in a PowerShell string literal?


How can I encode the Unicode character U+0048 (H), say, in a PowerShell string?

In C# I would just do this: "\u0048", but that doesn't appear to work in PowerShell.


Solution

  • Replace '\u' with '0x' and cast it to System.Char:

    PS > [char]0x0048
    H
    

    You can also use the "$()" syntax to embed a Unicode character into a string:

    PS > "Acme$([char]0x2122) Company"
    AcmeT Company
    

    Where T is PowerShell's representation of the character for non-registered trademarks.

    Note: this method works only for characters in Plane 0, the BMP (Basic Multilingual Plane), chars < U+10000.