GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Clear output encodings before the ShellExecute fallback for native commands by b84330808 · Pull Request #28327 · PowerShell/PowerShell

Conversation

@b84330808

@b84330808 Wei-Lun Li (b84330808) commented Nov 4, 2026

Copy link
Copy Markdown

PR Summary

Fixes #28225.

When a native command fails to start on Windows and it is the only command in the pipeline, PowerShell retries with ShellExecute. That retry turns off the stream redirection but leaves StandardOutputEncoding / StandardErrorEncoding set, so Process.Start throws before ShellExecute is tried:

Set-Content notpe.exe 'not an executable'
$x = & .\notpe.exe
# Program 'notpe.exe' failed to run: StandardOutputEncoding is only supported when standard output is redirected.
& .\notpe.exe 2>$null
# Program 'notpe.exe' failed to run: StandardErrorEncoding is only supported when standard error is redirected.

This change clears both encodings together with the redirection flags. With it, both forms report the real start error, the same as & .\notpe.exe | Out-Null.

PR Context

The encodings were added for redirected native output, but the ShellExecute fallback was not updated to clear them, so any captured native command that needs the fallback fails with an unrelated InvalidOperationException instead.

The new test (Windows only) runs a non-PE .exe with stdout captured and with stderr redirected, and checks that the error's inner exception is the Win32Exception from starting the process. It fails without the fix.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge. If this PR is a work in progress, please open this as a Draft Pull Request and mark it as Ready to Review when it is ready to merge.
  • Breaking changes
    • None
    • OR
    • Experimental feature(s) needed
      • Experimental feature name(s):
  • User-facing changes
    • Not Applicable
    • OR
    • Documentation needed
      • Issue filed:
  • Testing - New and feature
    • N/A or can only be tested interactively
    • OR
    • Make sure you've added a new test if existing tests do not effectively test the code changed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@b84330808
Clear output encodings before the ShellExecute fallback for native co…
8ca629f
…mmands

When a native command whose stdout or stderr is captured fails to start,
the ShellExecute fallback turns off the stream redirection but keeps
`StandardOutputEncoding` and `StandardErrorEncoding`. `Process.Start`
then throws "StandardOutputEncoding is only supported when standard
output is redirected", so ShellExecute is never tried and the real
start error is lost.

Clear both encodings together with the redirection flags.

Fixes PowerShell#28225
@b84330808
Wei-Lun Li (b84330808) requested a review from a team as a code owner November 4, 2026 06:20
Copilot AI balanced review requested due to automatic review settings November 4, 2026 06:20
@azure-pipelines

azure-pipelines Bot commented Nov 4, 2026

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Copilot started reviewing on behalf of Wei-Lun Li (b84330808) November 4, 2026 06:21
View session
Copilot AI reviewed Nov 4, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

🟢 Approval recommended

No active review issues were identified in the change to preserve native-command launch errors during the Windows ShellExecute fallback.

No open issues 🎉

What changed in this PR

Fixes #28225 by clearing output encodings before Windows retries a native command with ShellExecute, preventing an encoding validation error from masking the launch failure.

Changes:

  • Clear standard output and error encodings when disabling redirection for the fallback.
  • Add Windows-only regression tests for captured stdout and redirected stderr, checking that failures expose a Win32Exception.
File Description
test/​powershell/​Language/​Scripting/​NativeExecution/​NativeCommandProcessor.Tests.ps1 Tests launch-error reporting for both redirection scenarios.
src/​System.Management.Automation/​engine/​NativeCommandProcessor.cs Clears output encodings before the ShellExecute retry.

Review effort: Balanced

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Nov 11, 2026
@microsoft-github-policy-service

microsoft-github-policy-service Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Captured native command that fails to start reports 'StandardOutputEncoding is only supported when standard output is redirected' instead of the real launch error

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@b84330808
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.