Marking Parameter as Flag Value

Welcome Forums General PowerShell Q&A Marking Parameter as Flag Value

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 week, 1 day ago.

  • Author
    Posts
  • #181833

    Participant
    Topics: 1
    Replies: 1
    Points: -4
    Rank: Member

    Hi, how do I mark a parameter as a flag? I use the Parameter decorator right now, but if I give the variable a type of bool this means I have to specify $true or $false after it.

    I'm doing this from C#, but I could not find examples for Psh either.

  • #181884

    Participant
    Topics: 2
    Replies: 497
    Points: 1,230
    Helping Hand
    Rank: Community Hero

    In PowerShell, you would type this parameter as [switch]. In C# you need to use the SwitchParameter type. 🙂

  • #182022

    Participant
    Topics: 1
    Replies: 1
    Points: -4
    Rank: Member

    Thanks for the response Joel. To clarify for anyone reading this later, the parameter's type is SwitchParameter, it's not an annotation. Confused me at first.

    As a follow up question, this marks the flag as [[-Name]] instead of [-Name] when using Get-Help. Is there a way to fix this?

  • #182466

    Participant
    Topics: 10
    Replies: 84
    Points: 273
    Helping Hand
    Rank: Contributor

    Specifying the Position in the [Parameter()] decorator will cause that. Removing the Position will get you [-Name].

  • #182514

    Participant
    Topics: 2
    Replies: 497
    Points: 1,230
    Helping Hand
    Rank: Community Hero

    Yep. Switch parameters shouldn't declare position anyway, since you can't actually give them a value positionally.

You must be logged in to reply to this topic.