Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Severity Level: Warning
Description
This rule detects inconsistent parameter set naming and configuration issues that can cause runtime errors. Parameter set names in PowerShell are case-sensitive, unlike most other PowerShell elements. With this rule enabled, it's easier to debug your functions. This rule is disabled by default.
This rule performs five different checks:
- Missing DefaultParameterSetName - Warns when parameter sets are used but no default is specified.
- Multiple parameter declarations - Detects when a parameter is declared multiple times in the same parameter set. This issue becomes a runtime exception, so this check catches it earlier.
- Case mismatch between DefaultParameterSetName and ParameterSetName - Ensures consistent casing.
- Case mismatch between different ParameterSetName values - Ensures all references to the same parameter set use identical casing.
- Parameter set names containing newlines - Warns against using newline characters in parameter set names.
Guidance
- Use a
DefaultParameterSetNamewhen defining multiple parameter sets - Ensure consistent casing between
DefaultParameterSetNameandParameterSetNamevalues - Use identical casing for all references to the same parameter set name
- Avoid declaring the same parameter multiple times in a single parameter set
- Don't use newline characters in parameter set names
Notes
- The first occurrence of a parameter set name in your code is treated as the canonical casing
- Parameters without
[Parameter()]attributes are automatically part of all parameter sets - It's a PowerShell best practice to always specify a
DefaultParameterSetNamewhen you're using parameter sets
Example
Noncompliant
# Missing DefaultParameterSetName
function Get-Data {
[CmdletBinding()]
param(
[Parameter(ParameterSetName='ByName')]
[string]$Name,
[Parameter(ParameterSetName='ByID')]
[int]$ID
)
}
# Case mismatch between DefaultParameterSetName and ParameterSetName
function Get-Data {
[CmdletBinding(DefaultParameterSetName='ByName')]
param(
[Parameter(ParameterSetName='byname')]
[string]$Name,
[Parameter(ParameterSetName='ByID')]
[int]$ID
)
}
# Inconsistent casing between ParameterSetName values
function Get-Data {
[CmdletBinding(DefaultParameterSetName='ByName')]
param(
[Parameter(ParameterSetName='ByName')]
[string]$Name,
[Parameter(ParameterSetName='byname')]
[string]$DisplayName
)
}
# Multiple parameter declarations in same set
function Get-Data {
param(
[Parameter(ParameterSetName='ByName')]
[Parameter(ParameterSetName='ByName')]
[string]$Name
)
}
# Parameter set name with newline
function Get-Data {
param(
[Parameter(ParameterSetName="Set`nOne")]
[string]$Name
)
}
Compliant
# Proper parameter set configuration
function Get-Data {
[CmdletBinding(DefaultParameterSetName='ByName')]
param(
[Parameter(ParameterSetName='ByName', Mandatory)]
[string]$Name,
[Parameter(ParameterSetName='ByName')]
[Parameter(ParameterSetName='ByID')]
[string]$ComputerName,
[Parameter(ParameterSetName='ByID', Mandatory)]
[int]$ID
)
}
Configuration
Rules = @{
PSUseConsistentParameterSetName = @{
Enable = $true
}
}
Parameters
Enable
This parameter controls whether ScriptAnalyzer checks the code against this rule. It accepts a
boolean value. To enable this rule, set this parameter to $true. The default value is $false.