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

PowershellUnicodeString LiteralsUnicode Literals

Powershell Problem Overview


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.

Powershell Solutions


Solution 1 - Powershell

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.

Solution 2 - Powershell

According to the documentation, PowerShell Core 6.0 adds support with this escape sequence:

PS> "`u{0048}"
H

see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-6#unicode-character-ux

Solution 3 - Powershell

Maybe this isn't the PowerShell way, but this is what I do. I find it to be cleaner.

[regex]::Unescape("\u0048") # Prints H
[regex]::Unescape("\u0048ello") # Prints Hello

Solution 4 - Powershell

Another way using PowerShell.

$Heart = $([char]0x2665)
$Diamond = $([char]0x2666)
$Club = $([char]0x2663)
$Spade = $([char]0x2660)
Write-Host $Heart -BackgroundColor Yellow -ForegroundColor Magenta

Use the command help Write-Host -Full to read all about it.

Solution 5 - Powershell

For those of us still on 5.1 and wanting to use the higher-order Unicode charset (for which none of these answers work) I made this function so you can simply build strings like so:

'this is my favourite park ',0x1F3DE,'. It is pretty sweet ',0x1F60A | Unicode

enter image description here

#takes in a stream of strings and integers,
#where integers are unicode codepoints,
#and concatenates these into valid UTF16
Function Unicode {
	Begin {
		$output=[System.Text.StringBuilder]::new()
	}
	Process {
		$output.Append($(
			if ($_ -is [int]) { [char]::ConvertFromUtf32($_) }
			else { [string]$_ }
		)) | Out-Null
	}
	End { $output.ToString() }
}

Note that getting these to display in your console is a whole other problem, but if you're outputting to an Outlook email or a Gridview (below) it will just work (as utf16 is native for .NET interfaces).

enter image description here

This also means you can also output plain control (not necessarily unicode) characters pretty easily if you're more comfortable with decimal since you dont actually need to use the 0x (hex) syntax to make the integers. 'hello',32,'there' | Unicode would put a non-breaking space betwixt the two words, the same as if you did 0x20 instead.

Solution 6 - Powershell

To make it work for characters outside the BMP you need to use Char.ConvertFromUtf32()

'this is my favourite park ' + [char]::ConvertFromUtf32(0x1F3DE) + 
'. It is pretty sweet ' + [char]::ConvertFromUtf32(0x1F60A)

Solution 7 - Powershell

Note that some characters like  might need a "double rune" to be printed:

   PS> "C:\foo\bar\$([char]0xd83c)$([char]0xdf0e)something.txt"

Will print:

   C:\foo\bar\🌎something.txt

You can find these "runes" here, in the "unicode escape" row:

   https://dencode.com/string

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondan-gphView Question on Stackoverflow
Solution 1 - PowershellShay LevyView Answer on Stackoverflow
Solution 2 - PowershellmclaytonView Answer on Stackoverflow
Solution 3 - PowershellKevin BuchanView Answer on Stackoverflow
Solution 4 - PowershelllitView Answer on Stackoverflow
Solution 5 - PowershellHashbrownView Answer on Stackoverflow
Solution 6 - PowershellphuclvView Answer on Stackoverflow
Solution 7 - PowershellXDSView Answer on Stackoverflow