What’s the deal with Powershell commandlet switch parameters that require a colon?
Consider Exchange 2010 management shell cmdlet Move-ActiveMailboxDatabase. The Confirm switch is a System.Management.Automation.SwitchParameter and must be used like so,
Move-ActiveMailboxDatabase -Confirm:$false
Without the colon the command fails to recognize the don’t confirm switch like so,
Move-ActiveMailboxDatabase -Confirm $false
Why is that? What’s the difference the colon makes there? Why Exchange2010 seems to be about the only thing I’ve noticed this behavior?
I’ve browsed through Powershell in Action and Powershell 2.0, but didn’t find anything about this syntax. Scope resolution and .Net object access uses are documented on those books though.
My Google-fu found an article which claims that it explicitly forwards switch parameter values, but fails to explain what that is about.
When you do:
you are not saying
Confirmparameter accepts the$false. You are saying-Confirmand also passing an (separate) argument to the cmdlet with value$false.Since
Confirmis a switch, just the presence of-Confirmmeans it is true. Absence of-Confirmmeans it is false.Let me give you a script example:
If you just run the script without any arguments / paramters i.e
.\script.ps1you get output:If you run it as
.\script.ps1 -test, the output isIf you run it as
.\script.ps1 -test $false, the output isIf you run it as
.\script.ps1 -test:$falsethe output isIt is in scenarios where the value for a switch variable itself has to be determined from another variable that the
:is used.For example, consider the script:
Here if you run it as
.\script.ps1 -in $false, you getIf you weren’t able to use the
:, you would have had to write it as: