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

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of I Am Sir Ask Alot I Am Sir Ask Alot 1 year, 6 months ago.

  • Author
    Posts
  • #26932
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

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

    Thanks Don

You must be logged in to reply to this topic.