Skip to content

Respect -UseWindowsPowerShell:$false in New-PSSession#26469

Merged
iSazonov merged 4 commits intoPowerShell:masterfrom
yotsuda:fix/new-pssession-usewindowspowershell-26464
Nov 21, 2025
Merged

Respect -UseWindowsPowerShell:$false in New-PSSession#26469
iSazonov merged 4 commits intoPowerShell:masterfrom
yotsuda:fix/new-pssession-usewindowspowershell-26464

Conversation

@yotsuda
Copy link
Contributor

@yotsuda yotsuda commented Nov 17, 2025

PR Summary

This PR fixes the issue where -UseWindowsPowerShell:$false is not respected in New-PSSession, causing it to behave the same as -UseWindowsPowerShell:$true.

Fixes #26464
Related to #25242

PR Context

When -UseWindowsPowerShell:$false is explicitly specified for New-PSSession, the cmdlet incorrectly creates a Windows PowerShell 5.1 session instead of a default PowerShell Core session. This occurs because the code checks the parameter set name (which is selected based on the presence of the switch, not its value) instead of checking the actual boolean value of the UseWindowsPowerShell parameter.

This fix changes the condition to directly check the UseWindowsPowerShell property value, ensuring that explicit $false values are properly respected.

Changes

  • Modified newrunspacecommand.cs line 464: Added if (UseWindowsPowerShell) check in the UseWindowsPowerShellParameterSet case
  • Added test case in PSSession.Tests.ps1 to verify -UseWindowsPowerShell:$false creates a default PowerShell Core session

Testing

  • Added new test: "Should create default PowerShell session when -UseWindowsPowerShell:$false is used"
  • The test verifies that -UseWindowsPowerShell:$false creates a session with ShellUri matching the default PowerShell endpoint
  • Pre-fix: Test fails (creates Windows PowerShell 5.1 session with ShellUri ending in "Microsoft.PowerShell.Workflow")
  • Post-fix: Test passes (creates default session with ShellUri ending in "Microsoft.PowerShell")
  • All existing tests continue to pass

PR Checklist

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 fixes a bug where New-PSSession -UseWindowsPowerShell:$false was incorrectly creating a Windows PowerShell 5.1 session instead of a default PowerShell Core session. The issue occurred because the code checked the parameter set name (selected based on parameter presence) rather than the actual boolean value of the switch parameter.

  • Fixed parameter value handling in newrunspacecommand.cs by checking the actual UseWindowsPowerShell boolean value
  • Added test coverage to verify -UseWindowsPowerShell:$false creates the expected default session type

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/System.Management.Automation/engine/remoting/commands/newrunspacecommand.cs Added conditional check on UseWindowsPowerShell value to properly handle explicit $false, falling back to ComputerName parameter set behavior
test/powershell/engine/Remoting/PSSession.Tests.ps1 Added test to verify -UseWindowsPowerShell:$false produces the same behavior as not specifying the parameter

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@daxian-dbw daxian-dbw added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 17, 2025
@daxian-dbw
Copy link
Member

daxian-dbw commented Nov 18, 2025

@yotsuda The new test failed in CI run: https://github.com/PowerShell/PowerShell/actions/runs/19446455571/job/55648365344?pr=26469#step:3:1406. Please address the failure.

GitHub
PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.

- Adopt { ... } | Should -Not -Throw pattern as suggested by @mklement0
- Verify Transport is 'WSMan' (not 'Process') for -UseWindowsPowerShell:$false
- Simpler test that is independent of default PowerShell edition/version
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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

1. Rename $session1 to $sessionWithTrue for better symmetry with $sessionWithFalse
2. Add null check for $sessionWithFalse to match the pattern used in Test 1

These changes improve test code readability and robustness.
@iSazonov iSazonov merged commit e81985d into PowerShell:master Nov 21, 2025
40 checks passed
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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@yotsuda yotsuda deleted the fix/new-pssession-usewindowspowershell-26464 branch November 21, 2025 23:01
@iSazonov iSazonov self-assigned this Nov 22, 2025
SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-UseWindowsPowerShell:$false not respected in New-PSSession

5 participants