| description | Avoid Default Value For Mandatory Parameter |
|---|---|
| ms.date | 07/21/2026 |
| ms.topic | reference |
| title | AvoidDefaultValueForMandatoryParameter |
Severity Level: Warning
Default state: Always enabled
This rule detects when mandatory parameters have default values assigned. Mandatory parameters shouldn't have default values because they can't be used. When a parameter is marked as mandatory, PowerShell always prompts the user for a value if one isn't supplied when calling the function. Any default value assigned to a mandatory parameter is unreachable and serves no purpose.
function Test
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
$Parameter1 = 'default Value'
)
}function Test
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
$Parameter1
)
}This rule is always enabled and isn't configurable. Use one of the following methods to avoid using this rule:
- Create a custom rule configuration file to include only the rules you want or exclude the rules you don't want.
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific code blocks. For more information, see the Suppressing rules section of Using PSScriptAnalyzer.