-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add PSApplicationOutputEncoding variable #21219
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
Conversation
|
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) |
a202534 to
faaf11f
Compare
faaf11f to
f1d187e
Compare
f1d187e to
661e310
Compare
daxian-dbw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
|
@jborean93 Please rebase to get latest build workflows. |
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.
661e310 to
f492cfc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces the $PSApplicationOutputEncoding variable to provide a scoped, thread-safe mechanism for controlling the encoding PowerShell uses when reading output from native applications, addressing the limitations of using the process-wide [Console]::OutputEncoding setting.
Key Changes:
- Adds
$PSApplicationOutputEncodingvariable withEncodingtype converter - Modifies native command processor to use the new variable when set, falling back to
[Console]::OutputEncoding - Includes comprehensive test coverage for the new functionality
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
SpecialVariables.cs |
Defines the new PSApplicationOutputEncoding constant and variable path for internal reference |
InitialSessionState.cs |
Registers the $PSApplicationOutputEncoding session state variable with appropriate type converter |
RunspaceInit.resx |
Adds user-facing description for the new variable |
NativeCommandProcessor.cs |
Implements GetOutputEncoding() method to retrieve encoding from variable or fallback to console default |
NativeCommandEncoding.Tests.ps1 |
Provides test coverage for variable scoping, null handling, and type validation |
WriteConsoleOut.ps1 |
Test asset that writes encoded bytes to console for testing encoding behavior |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
@iSazonov, the PR has been rebased. |
|
📣 Hey @@jborean93, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗 https://aka.ms/PSRepoFeedback |
|
Thanks for the review and merge everyone! |
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.
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.
PR Summary
Adds the
$PSApplicationOutputEncodingvariable 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]::OutputEncodingvalue is used to preserve the existing behaviour.I ended up with
$PSApplicationOutputEncodingto reflect that this is for the "Application" commands in PowerShell (Get-Command ... -CommandType Application) and$OutputEncodingis 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]::OutputEncodingto 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.exewhich 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]::OutputEncodingand 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,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).