-1

My variables are not properly displaying if they contain non-ASCII characters:

$Test = '♭'
$Test

Expected output: Actual output: â™­

In an old batch script I could fix it by running this command at the top of my script: chcp 65001 However, it doesn't work in PowerShell and I can't find anything like it.

How can I properly set non-ASCII values for my variables?

My .ps1 file is UTF-8 encoded, so the problem isn't there.

GFL
  • 1,278
  • 2
  • 15
  • 24
  • 1
    If you have the unicode encoding you can write `[char]0xcode`. – harper May 24 '19 at 04:21
  • It worked as expected for me on PS 5.1 ISE. – Sid May 24 '19 at 04:30
  • 2
    Possible duplicate of [Displaying Unicode in Powershell](https://stackoverflow.com/questions/49476326/displaying-unicode-in-powershell) – Moerwald May 24 '19 at 04:56
  • @RohinSidharth it worked in ISE for me too, but not when I run the .ps1 script. – GFL May 24 '19 at 05:18
  • @Moerwald that page deals with font issues. That's not my issue. I also tried what they suggested but it did not resolve my issue. – GFL May 24 '19 at 05:19
  • Try out some of the answers here (even the ones at the bottom, there are a few varieties). https://superuser.com/questions/269818/change-default-code-page-of-windows-console-to-utf-8 – Dhruv Murarka May 24 '19 at 05:46
  • This answer looks promising: https://stackoverflow.com/questions/49476326/displaying-unicode-in-powershell – Dhruv Murarka May 24 '19 at 05:52
  • So the question is, "how do I display unicode characters in the powershell console"? – js2010 May 24 '19 at 14:47
  • It works in the ise or osx, like in my deleted answer. But I can't get it to work in the console in windows so far, despite all these links. – js2010 May 24 '19 at 21:43

1 Answers1

0

I've confirmed that these fonts display the "flat sign" ok in the console, with no other changes: MS Gothic, NSimSun, SimSum-ExtB. Although the blackslash looks strange in MS Gothic.

Strange how Consolas and Lucida Console work ok in the ISE.

It works in the VSCode console too by the way, even though $outputencoding is set to ascii.

I tried this to mimic the ISE, but it says unknown type.

$OutputEncoding = New-Object System.Text.SBCSCodePageEncoding
js2010
  • 23,033
  • 6
  • 64
  • 66