New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PSApplicationOutputEncoding variable #21219
base: master
Are you sure you want to change the base?
Conversation
Adds the $PSApplicationOutputEncoding variable that can be used to control the encoding PowerShell uses when reading the raw bytes to a string of an external application. This provides a scoped option to control the encoding used without resorting to the process wide setting of [Console]::OutputEncoding.
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
PR Summary
Adds the
$PSApplicationOutputEncoding
variable that can be used to control the encoding PowerShell uses when reading the raw bytes to a string of an external application. This provides a scoped option to control the encoding used without resorting to the process wide setting of[Console]::OutputEncoding
.If the variable is unset (the default) or
$null
, the[Console]::OutputEncoding
value is used to preserve the existing behaviour.I ended up with
$PSApplicationOutputEncoding
to reflect that this is for the "Application" commands in PowerShell (Get-Command ... -CommandType Application
) and$OutputEncoding
is already taken as the confusingly named option for controlling how PowerShell encodes input to an application.While not covered in this PR this could potentially be expanded in the future with:
$PSApplicationOutputEncoding = 'utf-8'
byte[]
without any encodingPR Context
#16868 for this specific ask but there are many related issues to this.
Currently PowerShell uses the value of
[Console]::OutputEncoding
to control what encoding is used when reading the output from a native application. This can be problematic as:A very common example is using
wsl.exe
which is hardcoded to output as UTF-16-LE/Unicode. To do this in PowerShell you would need to do:With this PR you can now do
Not only is this less lines, it is thread safe so you can run this in parallel in multiple runspaces at the same time and it also won't change how PowerShell might output it's strings to the console. The scriptblock can even be omitted if already running in a child scope that doesn't need to go back to the default.
Other known examples of external applications that don't follow the value of
[Console]::OutputEncoding
and typically need the user to set it when callingwinget.exe
- always uses UTF-8python.exe
- uses the Windows locale encoding (WinPS called this ANSI), or can be UTF-8 if an env var or argument is set to force itPR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).