Marking Parameter as Flag Value

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

Viewing 4 reply threads
  • Author
    Posts
    • #181833
      Participant
      Topics: 1
      Replies: 1
      Points: -3
      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: 510
      Points: 1,301
      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: -3
      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: 115
      Points: 444
      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: 510
      Points: 1,301
      Helping Hand
      Rank: Community Hero

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

Viewing 4 reply threads
  • You must be logged in to reply to this topic.