Forum Discussion
huntantr
Mar 11, 2021Copper Contributor
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...
- 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.
AndySvints
Mar 11, 2021Steel 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.
huntantr
Mar 23, 2021Copper Contributor
Thanks, this is what I was looking for.