vscode-code-runner: Running PowerShell, the output has many strange characters

173 views Asked by At

I use Visual Studio code with the Code Runner Extension to run PowerShell scripts. It works, but the output is very strange, full of unreadable characters.

German umlauts are not displayed (marked with red arrows in the following screenshot) and there are strange characters (blue arrows): screenshot 1

I tried to add a character set in settings.json for Code Runner with different code pages:

"code-runner.executorMap": { "powershell": "chcp 1252 & pwsh -ExecutionPolicy ByPass -File"}

But to no avail.

My script-files are all in utf8 and when I run PowerShell and the script from a terminal window, the output looks fine: screenshot 2

1

There are 1 answers

0
mklement0 On BEST ANSWER

Visual Studio Code has a dedicated PowerShell extension, which avoids the problem you're facing and is generally preferable due to providing rich support for PowerShell development and debugging.


If you still want to use the Code Runner Extension, the following may help (untested):

"code-runner.executorMap": { "powershell": "chcp 65001 & set NO_COLOR=1 & pwsh -ExecutionPolicy ByPass -File"}
  • chcp 65001 sets the active code page to 65001, i.e. UTF-8, which PowerShell then uses when emitting output.

  • Defining the NO_COLOR environment variable instructs PowerShell not to use ANSI/VT escape sequences to color its for-display formatted output.
    It is these escape sequences that Code Runner doesn't render as such, causing the artifacts highlighted with your blue arrows.
    See also: PowerShell's conceptual about_ANSI_Terminals help topic.