Skip to content

Respect -Shuffle:$false in Get-Random#26457

Merged
iSazonov merged 1 commit intoPowerShell:masterfrom
yotsuda:fix/get-random-shuffle-26456
Nov 17, 2025
Merged

Respect -Shuffle:$false in Get-Random#26457
iSazonov merged 1 commit intoPowerShell:masterfrom
yotsuda:fix/get-random-shuffle-26456

Conversation

@yotsuda
Copy link
Contributor

@yotsuda yotsuda commented Nov 16, 2025

PR Summary

This PR fixes the issue where -Shuffle:$false is not respected in Get-Random, causing it to behave the same as -Shuffle:$true.

Fixes #26456
Related to #25242

PR Context

When -Shuffle:$false is explicitly specified for Get-Random, the cmdlet incorrectly returns all items in shuffled order instead of returning a single random item. This occurs because the code checks ParameterSetName (a string) instead of checking the actual boolean value of the Shuffle parameter.

This fix changes the condition from checking the parameter set name to directly checking the Shuffle property value, ensuring that explicit $false values are properly respected.

Changes

  • Modified GetRandomCommandBase.cs line 491: Changed if (ParameterSetName == ShuffleParameterSet) to if (Shuffle)
  • Added test case in Get-Random.Tests.ps1 to verify -Shuffle:$false returns a single item

Testing

  • Added new test: "Should return a single random item when -Shuffle:$false is used"
  • The test verifies that -Shuffle:$false returns exactly one item from the input collection
  • Pre-fix: Test fails (returns 6 items instead of 1)
  • Post-fix: Test passes (returns 1 item as expected)
  • All existing tests continue to pass

PR Checklist

@iSazonov iSazonov requested a review from Copilot November 16, 2025 09:20
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 16, 2025
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 Get-Random incorrectly ignores the explicit -Shuffle:$false parameter, returning all items shuffled instead of a single random item. The fix changes the condition check from comparing the parameter set name to directly evaluating the Shuffle boolean property.

  • Changed the shuffle condition from checking ParameterSetName == ShuffleParameterSet to checking the actual Shuffle property value
  • Added a test case to verify -Shuffle:$false correctly returns a single random item

Reviewed Changes

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

File Description
src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommandBase.cs Fixed the shuffle condition to check the actual boolean value of the Shuffle property instead of the parameter set name, ensuring explicit -Shuffle:$false is respected
test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 Added test case to verify that -Shuffle:$false returns a single random item instead of all items shuffled

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

@iSazonov iSazonov self-assigned this Nov 16, 2025
@iSazonov iSazonov changed the title Fix: Respect -Shuffle:$false in Get-Random Respect -Shuffle:$false in Get-Random Nov 17, 2025
@iSazonov iSazonov merged commit 77f1d99 into PowerShell:master Nov 17, 2025
43 of 44 checks passed
@yotsuda yotsuda deleted the fix/get-random-shuffle-26456 branch November 18, 2025 00:27
SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
kilasuit pushed a commit to kilasuit/PowerShell that referenced this pull request Jan 2, 2026
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.

-Shuffle:$false not respected in Get-Random

3 participants