You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Console]::OutputEncoding doesn't work to parse exe with unicode output #10789
I have a Windows executable that produces unicode (utf-16) output. In PowerShell 5.1, I can set the [Console]::OutputEncoding property so the output of that command gets correctly interpreted. On PowerShell Core 6.2.3, that doesn't appear to work.
I've also tried setting [Console]::InputEncoding and $OutputEncoding, but the problem persists.
For example, I use the wsl.exe binary here, so this should repro on any system that has the Windows Subsystem for Linux installed.
PS C:\Users\svgroot> [Console]::OutputEncoding = [System.Text.Encoding]::Unicode
PS C:\Users\svgroot> wsl.exe --list -v | ForEach-Object { $_ }
NAME STATE VERSION
* Ubuntu Stopped 2
Ubuntu-18.04 Stopped 2
Alpine Stopped 1
Actual behavior
PS C:\Users\svgroot> [Console]::OutputEncoding = [System.Text.Encoding]::Unicode
PS C:\Users\svgroot> wsl.exe --list -v | ForEach-Object { $_ }
N A M E S T A T E V E R S I O N
* U b u n t u S t o p p e d 2
U b u n t u - 1 8 . 0 4 S t o p p e d 2
A l p i n e S t o p p e d 1
Environment data
Name Value
---- -----
PSVersion 6.2.3
PSEdition Core
GitCommitId 6.2.3
OS Microsoft Windows 10.0.19001
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
If I understand correctly, then, a fix should be to set the ProcessStartInfo.StandardInput(/Output)Encoding to match [console]::Input(/Output)Encoding values explicitly?
Should this respect [console] encoding settings, or $OutputEncoding? From what I recall, those values don't always align, if I'm not mistaken?
Agreed, 0xd4d: I don't know how .StandardInput comes into play, but on the output side It should definitely be [Console]::OutputEncoding, because that is how it has always worked in Windows PowerShell, where it determines how PowerShell decodes stream output from external programs.
$OutputEncoding controls what encoding is used to send data from Powershell to external programs, via a pipe. It defaults to UTF-8 in PSCore and to ASCII(!) in WinPS. In either edition it can differ from [Console]::OutputEncoding.
Michael Klement (@mklement0) I guess that would mean .StandardInputEncoding should match $OutputEncoding, then? 🤔 On the assumption that we may be piping into such a command as well.
Rain Sallow (/u/ta11ow) (@vexx32) I've only glanced at the code, and I see that the pipe that is connected to the child process' stdin explicitly uses $OutputEncoding:
When piping data from PowerShell to an external process, it is $OutputEncoding that already drives the standard input encoding for the child process (no change there - this was never broken).
When not piping (starting an interactive console application, for instance), i.e. when stdin is not redirected, we should set .StandardInput to [Console]::InputEncoding.
Whether redirected or not, .StandardOutput should always be set to [Console]::OutputEncoding
Michael Klement (@mklement0) when not piping, what is value of setting .StandardInput to any encoding? For your 3rd bullet, I believe you meant [Console]::OutputEncoding. For my PR, I'm focusing on output only unless someone brings a case where input encoding is a problem.
Steve Lee (@SteveL-MSFT): Thanks for the correction re 3rd bullet point - I've fixed my previous comment.
what is value of setting .StandardInput to any encoding?
My thinking is: An interactive console application that reads from stdin probably expects the console's (terminal's) input encoding to be in effect (that's presumably how it works in Windows PowerShell).
Activity
mklement0 commentedon Oct 16, 2019
That's not good - the bug is still present as of PowerShell Core 7.0.0-preview.4.
Here's a repro that doesn't require WSL:
As a Pester test:
vexx32 commentedon Oct 16, 2019
Has this ever worked in PowerShell Core, or has it been broken ever since 6.0.0?
mklement0 commentedon Oct 16, 2019
Rain Sallow (/u/ta11ow) (@vexx32): It's also broken in 6.0.0.
vexx32 commentedon Oct 16, 2019
If I understand correctly, then, a fix should be to set the
ProcessStartInfo.StandardInput(/Output)Encodingto match[console]::Input(/Output)Encodingvalues explicitly?Should this respect
[console]encoding settings, or$OutputEncoding? From what I recall, those values don't always align, if I'm not mistaken?mklement0 commentedon Oct 16, 2019
Agreed, 0xd4d: I don't know how
.StandardInputcomes into play, but on the output side It should definitely be[Console]::OutputEncoding, because that is how it has always worked in Windows PowerShell, where it determines how PowerShell decodes stream output from external programs.$OutputEncodingcontrols what encoding is used to send data from Powershell to external programs, via a pipe. It defaults to UTF-8 in PSCore and to ASCII(!) in WinPS. In either edition it can differ from[Console]::OutputEncoding.SvenGroot commentedon Oct 16, 2019
Thanks for looking into this, everyone. Hopefully this can get fixed soon.
jszabo98 commentedon Oct 17, 2019
Yep. Note that C:\Windows\system32\sfc.exe in Windows 10 outputs utf-16. It's a powershell question that comes up occasionally.
vexx32 commentedon Oct 17, 2019
Michael Klement (@mklement0) I guess that would mean
.StandardInputEncodingshould match$OutputEncoding, then? 🤔 On the assumption that we may be piping into such a command as well.mklement0 commentedon Oct 17, 2019
Rain Sallow (/u/ta11ow) (@vexx32) I've only glanced at the code, and I see that the pipe that is connected to the child process' stdin explicitly uses
$OutputEncoding:PowerShell/src/System.Management.Automation/engine/NativeCommandProcessor.cs
Lines 1797 to 1801 in 425bc36
I don't fully understand how that relates to the default
.StandardInputencoding - it looks like it may override it.SteveL-MSFT commentedon Oct 17, 2019
Sven Groot (@SvenGroot) do you have any examples on the input side where we have issues with encoding?
SvenGroot commentedon Oct 17, 2019
Steve Lee (@SteveL-MSFT) No, I only use OutputEncoding in my scenario.
mklement0 commentedon Oct 17, 2019
Here's my guess as to what we should do:
When piping data from PowerShell to an external process, it is
$OutputEncodingthat already drives the standard input encoding for the child process (no change there - this was never broken).When not piping (starting an interactive console application, for instance), i.e. when stdin is not redirected, we should set
.StandardInputto[Console]::InputEncoding.Whether redirected or not,
.StandardOutputshould always be set to[Console]::OutputEncodingSteveL-MSFT commentedon Oct 17, 2019
Michael Klement (@mklement0) when not piping, what is value of setting
.StandardInputto any encoding? For your 3rd bullet, I believe you meant[Console]::OutputEncoding. For my PR, I'm focusing on output only unless someone brings a case where input encoding is a problem.mklement0 commentedon Oct 17, 2019
Steve Lee (@SteveL-MSFT): Thanks for the correction re 3rd bullet point - I've fixed my previous comment.
My thinking is: An interactive console application that reads from stdin probably expects the console's (terminal's) input encoding to be in effect (
that's presumably how it works in Windows PowerShell).mklement0 commentedon Oct 26, 2019
Glad to see this was fixed for
.StandardOutput.As for setting
.StandardInputto[Console]::InputEncoding: please see #10907, Steve Lee (@SteveL-MSFT).