Skip to content

Files

Latest commit

 

History

History
40 lines (30 loc) · 894 Bytes

AvoidDefaultValueForMandatoryParameter.md

File metadata and controls

40 lines (30 loc) · 894 Bytes

Pattern: Use of default value for mandatory parameter

Issue: -

Description

Mandatory parameters should not have a default values because there is no scenario where the default can be used because PowerShell will prompt anyway if the parameter value is not specified when calling the function.

Example of incorrect code:

function Test
{

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        $Parameter1 = 'default Value'
    )
}

Example of correct code:

function Test
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        $Parameter1
    )
}

Further Reading