Suppose we have the following SwitchTest.ps script
param(
[switch]$Flag = $false
)
echo "Flag: $Flag"
The following works as expected:
& ".\SwitchTest.ps1" -Flag
Flag: True
However this doesn't
& ".\SwitchTest.ps1" "-Flag"
Flag: False
Furthermore, if we modify SwitchTest.ps to look like this:
param(
[switch]$Flag = $false,
[string]$Str = $null
)
echo "Flag: $Flag"
echo "Str: $Str"
We get this expected behavior:
& ".\SwitchTest.ps1" -Flag
Flag: True
Str:
And this unexpected one:
& ".\SwitchTest.ps1" "-Flag"
Flag: False
Str: -Flag
I tried -Flag true, -Flag $true, -Flag:$true... no cigar. It looks like the switch param is "skipped" when quoted (i.e. when it's a proper string) - why? What do I do if I want to build "-Flag" programmatically?
iexas @briantist suggested.