SOLVED

Adding a parameter to a script with no value

%3CLINGO-SUB%20id%3D%22lingo-sub-2203355%22%20slang%3D%22en-US%22%3EAdding%20a%20parameter%20to%20a%20script%20with%20no%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2203355%22%20slang%3D%22en-US%22%3E%3CP%3EIs%20there%20a%20way%20to%20add%20a%20parameter%20to%20a%20script%20that%20would%20work%20like%20-Force%20or%20-Verbose%3F%20I%20don't%20need%20to%20provide%20a%20value%2C%20I%20just%20want%20it%20to%20set%20a%20variable%20so%20that%20if%20set%20my%20script%20will%20do%20something%20extra.%3C%2FP%3E%3CP%3EOr%20is%20there%20a%20way%20that%20I%20could%20use%20the%20-Force%20parameter%3F%20Is%20there%20some%20script%20variable%20that%20would%20be%20set%20that%20I%20can%20check%20if%20it%20was%20used%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2203355%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2203728%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20a%20parameter%20to%20a%20script%20with%20no%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2203728%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F568825%22%20target%3D%22_blank%22%3E%40huntantr%3C%2FA%3E%2C%3C%2FP%3E%3CP%3EIf%20I%20understood%20you%20correctlly%2C%20you%20can%20use%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fabout%2Fabout_functions_advanced_parameters%3Fview%3Dpowershell-7.1%23switch-parameters%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESwitch%3C%2FA%3E%26nbsp%3Bparameter%20type%20in%20your%20script%3A%3C%2FP%3E%3CP%3E%3CEM%3ESwitch%20parameters%20are%20parameters%20with%20no%20parameter%20value.%20They're%20effective%20only%20when%20they're%20used%20and%20have%20only%20one%20effect.%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EFor%20example%20you%20can%20use%20something%20similar%20to%20this%3A%3C%2FSPAN%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Efunction%20Do-Magic%0A%7B%0A%20%20%20%20%5BCmdletBinding(SupportsShouldProcess%3D%24true)%5D%0A%20%20%20%20%5BAlias('magic')%5D%0A%20%20%20%20Param%0A%20%20%20%20(%0A%20%20%20%20%20%20%5BSwitch%5D%0A%20%20%20%20%20%20%24SkipRabbitTrick%0A%20%20%20%20)%0A%20%20%20%20Begin%7B%7D%0A%20%20%20%20Process%7B%0A%20%20%20%20%20%20%20if(%24SkipRabbitTrick)%7B%0A%20%20%20%20%20%20%20%20%20%20Write-Verbose%20%22Performance%20without%20Rabbit%22%0A%20%20%20%20%20%20%20%7Delse%7B%0A%20%20%20%20%20%20%20%20%20%20Write-Verbose%20%22Taking%20Rabbit%20out%20of%20Hat%22%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%7D%0A%20%20%20End%7B%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EHope%20that%20helps.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

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 extra.

Or is there a way that I could use the -Force parameter? Is there some script variable that would be set that I can check if it was used?

 

Thanks.

2 Replies
best response confirmed by huntantr (Occasional Contributor)
Solution

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.

 

Thanks, this is what I was looking for.