Powershell: Colon in commandlet parameters

前端 未结 2 1253
一向
一向 2020-12-17 15:25

What\'s the deal with Powershell commandlet switch parameters that require a colon?

Consider Exchange 2010 management shell cmdlet Move-ActiveMailboxDatabase. The Co

2条回答
  •  [愿得一人]
    2020-12-17 16:01

    The colon can be used with every parameter value but is more special in the case of switch parameters. Switch parameters don't take values, they are either present ($true) or absent ($false).

    Imagine you have a function like this:

    function test-switch ([string]$name,[switch]$force) { ... }
    

    And you call it like so:

    test-switch -force $false
    

    Since switch parameters are either present or not, $false would actually bind to the Name parameter. So, how do you bind a value to a switch parameter? With the colon:

    test-switch -force:$false
    

    Now the parameter binder knows which parameter the value goes to.

提交回复
热议问题