Forum Discussion

huntantr's avatar
huntantr
Copper Contributor
Mar 11, 2021

Adding a parameter to a script with no value

Is there a way to add a parameter to a script that would work like -Force or -Verbose? I don't need to provide a value, I just want it to set a variable so that if set my script will do something ext...
  • AndySvints's avatar
    Mar 11, 2021

    Hello huntantr,

    If I understood you correctlly, you can use Switch parameter type in your script:

    Switch parameters are parameters with no parameter value. They're effective only when they're used and have only one effect.

     

    For example you can use something similar to this:

    function Do-Magic
    {
        [CmdletBinding(SupportsShouldProcess=$true)]
        [Alias('magic')]
        Param
        (
          [Switch]
          $SkipRabbitTrick
        )
        Begin{}
        Process{
           if($SkipRabbitTrick){
              Write-Verbose "Performance without Rabbit"
           }else{
              Write-Verbose "Taking Rabbit out of Hat"
           }
       }
       End{}
    }

    Hope that helps.

     

Resources