Skip to content

New-Guid -Empty ignores an explicit $false value #25276

Closed

Description

@surfingoldelephant

Prerequisites

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 @params

Expected 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.

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.0

Activity

  1. jhoneill commented on Apr 4, 2025

    @jhoneill

    There will , no doubt, be someone who says "well don't set switches to false then"
    However if you have

    function mine { 
    Param {[switch]$mySwitch}
    
    some-cmdlet -SomeSwitch:MySwitch 
    } 
    

    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

  2. 237dmitry commented on Apr 4, 2025

    @237dmitry

    However if you have

    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'

  3. mklement0 commented on Apr 4, 2025

    @mklement0
    Contributor

    237dmitry: That is just a typo: it should be Param (...), not Param { ... }

    For the backstory, see:

    Since 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.

  4. 237dmitry commented on Apr 4, 2025

    @237dmitry

    it should be Param (...), not Param { ... }

    This is not working too:

    & {
      function mine { 
          Param ( [switch]$mySwitch )
         new-guid -empty:$mySwitch 
      }
      
      mine -mySwitch   # mine # mine -mySwitch # -mySwitch:$true or $false    
      }
    
  5. mklement0 commented on Apr 4, 2025

    @mklement0
    Contributor

    237dmitry, your example is working (with mine -mySwitch you're asking for an empty GUID and you're getting one), while mine (which makes $mySwitch implicitly $false in a Boolean context such as -empty:$mySwitch) and mine -mySwitch:$false aren't (you're still getting an empty GUID, despite negating the -Empty switch), which is what this issue is about.

  6. 237dmitry commented on Apr 4, 2025

    @237dmitry

    you're asking for an empty GUID and you're getting one

    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 $false

  7. moved this from Queue to In-Progress-Issues in Cmdlets Working Groupon May 7, 2025
  8. TobiasPSP commented on May 7, 2025

    @TobiasPSP
    Collaborator

    Cmdlet WG reviewed this. We agree this is a bug. This should not be a difficult fix.

  9. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    WG-ReviewedA Working Group has reviewed this and made a recommendation
    and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on May 7, 2025
  10. moved this from In-Progress-Issues to Reviewed in Cmdlets Working Groupon May 7, 2025
  11. microsoft-github-policy-service commented on Oct 6, 2025

    @microsoft-github-policy-service
    Contributor

    📣 Hey @surfingoldelephant, how did we do? We would love to hear your feedback with the link below! 🗣️

    🔗 https://aka.ms/PSRepoFeedback

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions