How to make PSCmdlet bool parameter to work like a flag?

939 views Asked by At

Using System.Management.Automation you can create custom PSCmdlets in C#. Now if you create boolean parameter like this:

[Parameter()]
public bool ShowDefinition { get; set; }

You have to invoke cmdlet like this:

PS> Get-CustomValues -ShowDefinition 1

But I would like to invoke it without passing value to -ShowDefinition. The same way as -Debug works. Like this:

PS> Get-CustomValues -ShowDefinition

How can I do this?

1

There are 1 answers

0
mswietlicki On

OK, I found the answer.

You have to use SwitchParameter.

[Parameter]
public SwitchParameter ShowDefinition { get; set; }

protected override void ProcessRecord(){
    if(ShowDefinition.ToBool()){
    ...
    }
}