Repository navigation
Closed
Labels
Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesgeneral cmdlet issuesWG-ReviewedA Working Group has reviewed this and made a recommendationA Working Group has reviewed this and made a recommendation
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
This is one of many instances of a shipped command that doesn't behave correctly when a switch parameter is explicitly passed a $false value.
For context, see:
Steps to reproduce
New-Guid -Empty:$false
# Equivalent, with hash table splatting:
$params = @{ Empty = $false }
New-Guid @paramsExpected behavior
New-Guid emits a non-empty [guid] instance. For example:
Guid
----
d2a83da7-6092-4c06-b8de-3b9aadbd8c94
Actual behavior
The $false value is ignored and an empty [guid] instance is emitted.
Guid
----
00000000-0000-0000-0000-000000000000
New-Guid uses the selected parameter set name (Empty), which is based on the presence of the switch parameter (not its value), to determine the behavior.
PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewGuidCommand.cs
Line 52 in f194359
| guid = ParameterSetName is "Empty" ? Guid.Empty : Guid.NewGuid(); |
Behavior should be determined by the value of the Empty property instead.
Environment data
Name Value
---- -----
PSVersion 7.6.0-preview.3
PSEdition Core
GitCommitId 7.6.0-preview.3
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Reacted by Michael Klement and James O'Neill
Metadata
Metadata
Assignees
Labels
Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesgeneral cmdlet issuesWG-ReviewedA Working Group has reviewed this and made a recommendationA Working Group has reviewed this and made a recommendation
Type
Projects
- StatusShow more project fieldsReviewed
Activity
$falsevalue passed to a switch parameter #25242jhoneill commentedon Apr 4, 2025
There will , no doubt, be someone who says "well don't set switches to false then"
However if you have
You rely on switches behaving properly with false.
There are plenty of bugs in the world where "present" and "evaluates to true" are wrongly assumed to be the same (e.g.
if ($color) {set-color $color}where color is an enum and 0=Black. So if black is requested the code doesn't run. Using a parameter set to infer parameter values is a new variation , but shouldn't be in a cmdlet.There may be other unnoticed ones
237dmitry commentedon Apr 4, 2025
I tried and it does not work.
The term 'Param' is not recognized...,Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by parameter 'Empty'mklement0 commentedon Apr 4, 2025
237dmitry: That is just a typo: it should be
Param (...), notParam { ... }For the backstory, see:
-Switch:$falsethe same as omitting a switch when selecting a parameter set #25027Since said proposal was rejected, the affected cmdlets must now be fixed one by one (these cmdlets were identified by surfingoldelephant in #25242).
Perhaps needless to say, not fixing the counterintuitive behavior in general will continue to trip up cmdlet authors, both third-party and Microsoft-internal ones.
237dmitry commentedon Apr 4, 2025
This is not working too:
mklement0 commentedon Apr 4, 2025
237dmitry, your example is working (with
mine -mySwitchyou're asking for an empty GUID and you're getting one), whilemine(which makes$mySwitchimplicitly$falsein a Boolean context such as-empty:$mySwitch) andmine -mySwitch:$falsearen't (you're still getting an empty GUID, despite negating the-Emptyswitch), which is what this issue is about.237dmitry commentedon Apr 4, 2025
No, although I don't like such a syntactic technique as explicit use of boolean values in switch variables, it looks, frankly, ugly, I just tried to see if it would work with passing another switch. It turned out that it doesn't work. The result is exactly the same as in the essence of the issue itself.
I check if it was like
$true -and $falseTobiasPSP commentedon May 7, 2025
Cmdlet WG reviewed this. We agree this is a bug. This should not be a difficult fix.
Added test of New-Guid -Empty:$false to New-Guid.Tests.ps1, reproduci…
microsoft-github-policy-service commentedon Oct 6, 2025
📣 Hey @surfingoldelephant, how did we do? We would love to hear your feedback with the link below! 🗣️
🔗 https://aka.ms/PSRepoFeedback