Search code examples
powershellunicodeencodingunicode-literals

Assign Unicode Literals to PowerShell Hash Table


It's late and I am done for tonight--can someone please lend a hand on getting this to work? Do I need to do some additional work to get these Unicode strings assigned as literals?

First attempt at creating a hash table of double-encoded and desired key-value pairs:

[string] $double_encoded_values = @{
"€"   = "€";
"‚"   = "‚";
"Æ’"    = "ƒ";
"„"   = "„";
"…"   = "…";
"â€"    = "†";
"‡"   = "‡";
"ˆ"    = "ˆ";
"‰"   = "‰";
"Å"     = "Š";
"‹"   = "‹";
"Å’"    = "Œ";
"Ž"    = "Ž";
"‘"   = "‘";
"’"   = "’";
"“"   = "“";
"â€"    = "”";
"•"   = "•";
"–"   = "–";
"—"   = "—";
"Ëœ"    = "˜" ;
"â„¢"   = "™";
"Å¡"    = "š";
"›"   = "›";
"Å“"    = "œ";
"ž"    = "ž";
"Ÿ"    = "Ÿ";
"¡"    = "¡";
"¢"    = "¢";
"£"    = "£";
"¤"    = "¤";
"Â¥"    = "¥";
"¦"    = "¦";
"§"    = "§";
"¨"    = "¨";
"©"    = "©";
"ª"    = "ª";
"«"    = "«";
"¬"    = "¬";
"®"    = "®";
"¯"    = "¯";
"°"    = "°";
"±"    = "±";
"²"    = "²";
"³"    = "³";
"´"    = "´";
"µ"    = "µ";
"¶"    = "¶";
"·"    = "·";
"¸"    = "¸";
"¹"    = "¹";
"º"    = "º";
"»"    = "»";
"¼"    = "¼";
"½"    = "½";
"¾"    = "¾";
} # $double_encoded_values

My version:

PS C:\Windows\system32> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1     

Error received:

At line:20 char:12
    + "–"   = "–";
    +            ~
    Missing '=' operator after key in hash literal.
    At line:20 char:12
    + "–"   = "–";
    +            ~
    The hash literal was incomplete.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEqualsInHashLiteral

Reference: http://www.i18nqa.com/debug/utf8-debug.html


Solution

  • PowerShell treats the in – as a double quote, effectively escaping the closing " in "–"

    Use single quotes to avoid having PowerShell trying to parse the key names and values:

    @{
        '–' = '-'
    }