Respect -Shuffle:$false in Get-Random#26457
Merged
iSazonov merged 1 commit intoPowerShell:masterfrom Nov 17, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
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 == ShuffleParameterSetto checking the actualShuffleproperty value - Added a test case to verify
-Shuffle:$falsecorrectly 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
approved these changes
Nov 16, 2025
This was referenced Nov 16, 2025
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
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR Summary
This PR fixes the issue where
-Shuffle:$falseis not respected inGet-Random, causing it to behave the same as-Shuffle:$true.Fixes #26456
Related to #25242
PR Context
When
-Shuffle:$falseis explicitly specified forGet-Random, the cmdlet incorrectly returns all items in shuffled order instead of returning a single random item. This occurs because the code checksParameterSetName(a string) instead of checking the actual boolean value of theShuffleparameter.This fix changes the condition from checking the parameter set name to directly checking the
Shuffleproperty value, ensuring that explicit$falsevalues are properly respected.Changes
GetRandomCommandBase.csline 491: Changedif (ParameterSetName == ShuffleParameterSet)toif (Shuffle)Get-Random.Tests.ps1to verify-Shuffle:$falsereturns a single itemTesting
-Shuffle:$falsereturns exactly one item from the input collectionPR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header