tl;dr
PowerShell does not support parameters with optional values.
A workaround is possible, but only for a single parameter.
Maximilian Burszley's helpful answer provides a workaround for a single parameter, via a catch-all parameter that collects all positionally passed arguments via the ValueFromRemainingArguments
parameter property.
Fundamentally, though, what you're asking for is unsupported in PowerShell:
PowerShell has no support for parameters with optional values as of 7.2 - except for [switch]
parameters, which are limited to [bool]
values.
That is:
Any parameter you declare with a type other than [switch]
invariably requires a value (argument).
The only other option is to indiscriminately collect any unbound positional arguments in a ValueFromRemainingArguments
-tagged parameter, but you won't be able to associate these with any particular other bound parameter.
In other words:
If you happen to need just one optional-argument parameter, the ValueFromRemainingArguments
can work for you (except that you should manually handle the case of mistakenly receiving multiple values), as shown in Maximilian Burszley's answer.
If you have two or more such parameters, the approach becomes impractical: you'd have to know in which order the parameters were passed (which PowerShell doesn't tell you) in order to associate the remaining positional arguments with the right parameters.
With [switch]
parameters (using an imagined -Quiet
switch as an example):
- The default value - if you just pass
-Quiet
-is $true
.
$false
is typically indicated by simply not specifying the switch at all (that is, omitting -Quiet
)
However, you may specify a value explicitly by following the switch name with :
, followed by the Boolean value:
-Quiet:$true
is the same as just -Quiet
-Quiet:$false
is typically the same as omitting -Quiet
; in rare cases, though, commands distinguish between an omitted switch and one with an explicit $false
value; notably, the common -Confirm
parameter allows use of -Confirm:$false
- as opposed to omission of -Confirm
- to override the value of the $ConfirmPreference
preference variable.
While :
as the separator between the parameter name and its argument (as opposed to the usual space char.) is supported with all parameters, with [switch]
parameters it is a must so as to unequivocally signal that what follows is an argument for the switch parameter (which by default needs no argument) rather than an independent, positional argument.
The above tells us that PowerShell already has the syntax for general support of optional-argument parameters, so at some point in the future it could support them with any data type, as suggested in GitHub issue #12104.
$text2
and addPosition = 0
to the first parameter. – Iila$Text
as astring
and aswitch
. – Topicalswitch
-Text
is passed, and a different behavior when they pass astring
to-Text
but powershell's rules around parametersets won't allow this use-case. – Topicalsed
's-i
(create a backup) option: if you pass just-i
, you opt into creating a backup with the default extension of.bak
; alternatively, you can pass the specific backup-file extension desired:-i.sav
. – Fermentation:
as the separator between parameter name and (optional) value, to unequivocally identify the next token as belonging to the parameter. In fact, that's how it already works for[switch]
parameters, but currently only for them. – Fermentation