Using [switch] parameter and mandatory=$false together?

Welcome Forums General PowerShell Q&A Using [switch] parameter and mandatory=$false together?

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

 
Participant
3 years, 5 months ago.

  • Author
    Posts
  • #26932

    Participant
    Points: 1
    Rank: Member

    Here is an excerpt from my script:

    [Parameter(Mandatory = $false)]
    [Alias('FCOP')]
    [switch]
    $OutputPath

    Do I need to do this, or does using the [switch] method basically make the "Mandatory=$false" attribute obsolete since using switch is basically telling it using the parameter would equal true, or not using it would make it false?

    Thanks

  • #26933

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    It doesn't "automatically" make Mandatory=$false, but Mandatory=$false is the default for all parameters (never a reason to specify it, really), and Mandatory=$true is incompatible with [switch] from a functional perspective.

  • #26934

    Participant
    Points: 1
    Rank: Member

    Okay, I thought $false was the default, but couldn't recall.

    Thanks Don

The topic ‘Using [switch] parameter and mandatory=$false together?’ is closed to new replies.