You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In short: What this issue proposes is to treat -Switch:$false as if -Switch had not been specified, as these two forms should generally be equivalent, with the former often being used for programmatically building arguments, especially via splatting.
Indeed, checking $Switch.IsPresent yields $false in both cases (for thoughts about the naming of this property, see this comment).
Currently, -SomeSwitch:$false acts the same as passing -SomeSwitch (i.e., implied :$true) in terms of parameter-set selection, which causes all cmdlets that only check whether the relevant parameter set was selected to in effect ignore the $false argument.
@MartinGC94's analysis here shows that quite a few existing cmdlets misbehave in this way, and the proposal at hand would presumably automatically fix that.
It is debatable whether this proposal should be considered a feature request or a bug report.
Either way, it is technically a breaking change, but - given the counterintuitive behavior and the presumed rarity of passing negated switches (-SomeSwitch:$false), it hopefully falls into bucket 3
In the vast majority of cases, omitting-SomeSwitch and passing -SomeSwitch:$false (which is the only way to emulate omitting the switch when splatting is used, short of conditionally constructing the splatting hashtable iteratively) should be considered equivalent.
(In edge cases, where -SomeSwitch:$false is designed to override a default value set elsewhere, notably via a preference variable (e.g., -Confirm:$false overriding the $ConfirmPreference variable), a different approach is already necessary, namely if, in addition to $Switch.IsPresent returning $false (which is the case when -Switch:$false is passed), whether this "non-presence" was explicitly signaled via a -Switch:$false argument rather than being implied by omission.)
To illustrate the problem:
functionfoo {
[CmdletBinding(DefaultParameterSetName='NoSwitchSet')]
param(
[Parameter(ParameterSetName='SwitchSet')]
[switch] $Switch
)
[pscustomobject] @{
SwitchValue=$SwitchParameterSetName=$PSCmdlet.ParameterSetName
}
}
# Invocation without -Switch
foo
# Invocation with negated -Switch, which _should_ be equivalent.
foo -Switch:$false# Ditto, via splatting$splat=@{ Switch=$false }
foo @splat
Summary of the new feature / enhancement
Note:
The problem affects cmdlets that use
[switch]parameters as mandatory in the context of an explicit parameter set, such asGet-UpTimeGet-Uptime -Sinceignores an explicit$falsevalue #25015-Switch:$falseas if-Switchhad not been specified, as these two forms should generally be equivalent, with the former often being used for programmatically building arguments, especially via splatting.$Switch.IsPresentyields$falsein both cases (for thoughts about the naming of this property, see this comment).-SomeSwitch:$falseacts the same as passing-SomeSwitch(i.e., implied:$true) in terms of parameter-set selection, which causes all cmdlets that only check whether the relevant parameter set was selected to in effect ignore the$falseargument.It is debatable whether this proposal should be considered a feature request or a bug report.
Either way, it is technically a breaking change, but - given the counterintuitive behavior and the presumed rarity of passing negated switches (
-SomeSwitch:$false), it hopefully falls into bucket 3-SomeSwitchand passing-SomeSwitch:$false(which is the only way to emulate omitting the switch when splatting is used, short of conditionally constructing the splatting hashtable iteratively) should be considered equivalent.-SomeSwitch:$falseis designed to override a default value set elsewhere, notably via a preference variable (e.g.,-Confirm:$falseoverriding the$ConfirmPreferencevariable), a different approach is already necessary, namely if, in addition to$Switch.IsPresentreturning$false(which is the case when-Switch:$falseis passed), whether this "non-presence" was explicitly signaled via a-Switch:$falseargument rather than being implied by omission.)To illustrate the problem:
Expected / proposed behavior:
Current behavior:
That is, the
-Switch:$falseinvocations (whether directly or via splatting) unexpectedly still selected theSwitchSetparameter set.Proposed technical implementation details (optional)
No response