Skip to content

Conversation

@jborean93
Copy link
Collaborator

@jborean93 jborean93 commented Feb 14, 2024

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:

  • Argument converters to support setting from a string/integer $PSApplicationOutputEncoding = 'utf-8'
  • A custom "raw/byte" encoding to have PowerShell output the byte[] without any encoding

PR 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:

  • It's set process wide, multithreading code can impact each other
  • You need an annoying try/finally to temporarily set it to another value
  • It also changes how the current process itself might write the output if the end user forgets to reset back to the default

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:

$origEncoding = [Console]::OutputEncoding
try {
    [Console]::OutputEncoding = [System.Text.Encoding]::Unicode
    $res = wsl.exe status
}
finally {
    [Console]::OutputEncoding = $origEncoding
}

With this PR you can now do

$res = & {
    $PSApplicationOutputEncoding = [System.Text.Encoding]::Unicode
    wsl.exe status
}

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 calling

  • winget.exe - always uses UTF-8
  • python.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 it

PR Checklist

@pull-request-quantifier-deprecated

This PR has 74 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Small
Size       : +72 -2
Percentile : 29.6%

Total files changed: 6

Change summary by file extension:
.cs : +14 -2
.resx : +3 -0
.ps1 : +55 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@pull-request-quantifier-deprecated

This PR has 78 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Small
Size       : +76 -2
Percentile : 31.2%

Total files changed: 6

Change summary by file extension:
.cs : +18 -2
.resx : +3 -0
.ps1 : +55 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Feb 21, 2024
@SeeminglyScience SeeminglyScience added WG-NeedsReview Needs a review by the labeled Working Group WG-Engine core PowerShell engine, interpreter, and runtime labels Dec 4, 2024
@SeeminglyScience SeeminglyScience added WG-Reviewed A Working Group has reviewed this and made a recommendation WG-NeedsReview Needs a review by the labeled Working Group and removed WG-NeedsReview Needs a review by the labeled Working Group WG-Reviewed A Working Group has reviewed this and made a recommendation labels Jan 13, 2025
@powercode powercode added WG-Reviewed A Working Group has reviewed this and made a recommendation Review - Needed The PR is being reviewed and removed Review - Needed The PR is being reviewed WG-NeedsReview Needs a review by the labeled Working Group labels Oct 20, 2025
Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Oct 21, 2025
@iSazonov
Copy link
Collaborator

@jborean93 Please rebase to get latest build workflows.

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label Oct 21, 2025
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.
Copilot AI review requested due to automatic review settings October 21, 2025 04:43
Copy link
Contributor

Copilot AI left a 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 $PSApplicationOutputEncoding variable with Encoding type 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.

@jborean93
Copy link
Collaborator Author

@iSazonov, the PR has been rebased.

@iSazonov iSazonov enabled auto-merge (squash) October 21, 2025 04:58
@iSazonov iSazonov merged commit af26292 into PowerShell:master Oct 21, 2025
34 checks passed
@microsoft-github-policy-service
Copy link
Contributor

microsoft-github-policy-service bot commented Oct 21, 2025

📣 Hey @@jborean93, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@jborean93 jborean93 deleted the console-encoding branch October 21, 2025 05:16
@jborean93
Copy link
Collaborator Author

Thanks for the review and merge everyone!

SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
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.
kilasuit pushed a commit to kilasuit/PowerShell that referenced this pull request Jan 2, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Small WG-Engine core PowerShell engine, interpreter, and runtime WG-Reviewed A Working Group has reviewed this and made a recommendation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants