Forum Discussion
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 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.
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.
2 Replies
- AndySvintsIron Contributor
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.
- huntantrCopper ContributorThanks, this is what I was looking for.