Understanding switch parameters

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of I Am Sir Ask Alot I Am Sir Ask Alot 2 years, 2 months ago.

  • Author
    Posts
  • #18968
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    I thought if you use a switch parameter you don't have to specify whether the parameter is true or false, but rather just using the parameter itself tells PowerShell that it is true.

    For instance, if I do something like in the following example, PowerShell responds with an error wanting the parameter to be specified explicitly as $false:

    Function Do-Something
    {
    param
    (
    [switch]param1
    )
    }

    Do-Something -param1

    However, if I do this, it works fine:

    Function Do-Something
    {
    param
    (
    [switch]param1
    )
    }

    Do-Something -param1 $true

    So we have to state that the parameter be true if we want it to be used? I thought just stating the parameter tells PowerShell that it is true?

    Any help it would be appreciated, as always.

    Thanks everyone

  • #18969
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Sorry, not explicitly as $false, but $true. That was a typo

  • #18970
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    For the most part, you're correct. Switch parameters, in their general usage, are $false if you don't specify them on the command line, and are $true if you do specify them on the command line. They don't take any arguments (again, in the general usage case):

    function MyFunction([switch] $MySwitch) { }
    
    MyFunction # $MySwitch is $false
    
    MyFunction -MySwitch # $MySwitch is $true
    

    However, sometimes you do need to be able to pass in a specific value for the switch parameter, either because you're passing it on from one function to another (such as in a proxy function), or because you need to deliberately set a switch to $false (which is rare, but you'll sometimes see this with -Confirm:$false . In this case, setting Confirm explicitly to false causes it to override the behavior of the $ConfirmPreference variable, which would otherwise be used.) When you do need to pass an argument to a switch parameter, you need place a colon between the parameter and its argument, like so:

    MyFunction -MySwitch:$false
    

    If you're using splatting with a hashtable, you can just assign a value of $true or $false to that key of the hashtable, and PowerShell knows what to do with it:

    $params = @{ MySwitch = $true }
    
    MyFunction @params
    
  • #18973
    Profile photo of Peter Jurgens
    Peter Jurgens
    Participant

    firstly, neither of those examples work for me from just copy/pasting as you've neglected the '$' in the parameter declaration so there is a general error and the function cannot be defined. The function should be like:

    Function Do-Something
    {
    param
    (
    [switch]$param1
    )
    }
    
    Do-Something -param1
    

    Can you provide the exact error message you're getting?

  • #18974
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Thanks Dave, but when I use the switch parameter I get the following error:

    [b]Missing an argument for parameter Param1.[/b]

    Peter,

    The code I used was just pseudo code, it was not my actual code.

    My actual code looks something like this:

    {Function Do-Something
    {
        [cmdletbinding()] 
         
        Param  
        (                 
             [Parameter(Mandatory=$false,  
             [switch[]]$Param1,
    
             [Parameter(Mandatory=$false, 
             [switch[]]$Param2,
    
             [Parameter(Mandatory=$false, 
             [switch[]]$Param3
        )   
    
    Begin {}
    
    End {}

    Please note, this also is not my identical code, but this is as close to it as possible.

    Thanks

  • #18975
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Okay, I found the issue. I used "[switch[]]" and after I removed the inner "[]" and just used "[switch]", it worked like intended.

    Can someone explain to me why the other way did not work? What are the inner "[]" used for?

    Thanks

  • #18977
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    [switch[]] would indicate an array of SwitchParameter objects, which doesn't really make any sense (though I suppose it's technically legal.)

  • #18997
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    oh i see

    Thanks again, Dave

You must be logged in to reply to this topic.