Skip to content

Commit ad591d0

Browse files
authored
Merge pull request #382 from sdwheeler/sdw-w563006-pssa125
Update PSSA for v1.25 release
2 parents 83d515b + 7381f00 commit ad591d0

9 files changed

Lines changed: 878 additions & 23 deletions
Lines changed: 139 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Align assignment statement
3-
ms.date: 06/28/2023
3+
ms.date: 03/20/2026
44
ms.topic: reference
55
title: AlignAssignmentStatement
66
---
@@ -10,38 +10,54 @@ title: AlignAssignmentStatement
1010

1111
## Description
1212

13-
Consecutive assignment statements are more readable if they are aligned. By aligned, we imply that
14-
the `equal` sign for all the assignment statements should be in the same column.
13+
Consecutive assignment statements are more readable when they're aligned. Assignments are considered
14+
aligned when their `equals` signs line up vertically.
1515

16-
The rule looks for key (property) value pairs in a hashtable (DSC configuration) to check if they
17-
are aligned or not. Consider the following example in which the key value pairs are not aligned.
16+
This rule looks at the key-value pairs in hashtables (including DSC configurations) as well as enum
17+
definitions.
18+
19+
Consider the following example with a hashtable and enum that isn't aligned.
1820

1921
```powershell
2022
$hashtable = @{
21-
property1 = 'value'
23+
property = 'value'
2224
anotherProperty = 'another value'
2325
}
26+
27+
enum Enum {
28+
member = 1
29+
anotherMember = 2
30+
}
2431
```
2532

2633
Alignment in this case would look like the following.
2734

2835
```powershell
2936
$hashtable = @{
30-
property1 = 'value'
37+
property = 'value'
3138
anotherProperty = 'another value'
3239
}
40+
41+
enum Enum {
42+
member = 1
43+
anotherMember = 2
44+
}
3345
```
3446

35-
The rule ignores hashtables in which the assignment statements are on the same line. For example,
36-
the rule ignores `$h = {a = 1; b = 2}`.
47+
The rule ignores any assignments within hashtables and enums which are on the same line as others.
48+
For example, the rule ignores `$h = @{a = 1; b = 2}`.
3749

3850
## Configuration
3951

4052
```powershell
4153
Rules = @{
4254
PSAlignAssignmentStatement = @{
43-
Enable = $true
44-
CheckHashtable = $true
55+
Enable = $true
56+
CheckHashtable = $true
57+
AlignHashtableKvpWithInterveningComment = $true
58+
CheckEnum = $true
59+
AlignEnumMemberWithInterveningComment = $true
60+
IncludeValuelessEnumMembers = $true
4561
}
4662
}
4763
```
@@ -52,8 +68,118 @@ Rules = @{
5268

5369
Enable or disable the rule during ScriptAnalyzer invocation.
5470

55-
#### CheckHashtable: bool (Default value is `$false`)
71+
#### CheckHashtable: bool (Default value is `$true`)
5672

5773
Enforce alignment of assignment statements in a hashtable and in a DSC Configuration. There is only
58-
one switch for hasthable and DSC configuration because the property value pairs in a DSC
74+
one setting for hashtable and DSC configuration because the property value pairs in a DSC
5975
configuration are parsed as key-value pairs of a hashtable.
76+
77+
#### AlignHashtableKvpWithInterveningComment: bool (Default value is `$true`)
78+
79+
Include key-value pairs in the alignment that have an intervening comment - that is to say a comment
80+
between the key name and the equals sign.
81+
82+
Consider the following:
83+
84+
```powershell
85+
$hashtable = @{
86+
property = 'value'
87+
anotherProperty <#A Comment#> = 'another value'
88+
anotherDifferentProperty = 'yet another value'
89+
}
90+
```
91+
92+
With this setting disabled, the line with the comment is ignored, and it would be aligned like so:
93+
94+
```powershell
95+
$hashtable = @{
96+
property = 'value'
97+
anotherProperty <#A Comment#> = 'another value'
98+
anotherDifferentProperty = 'yet another value'
99+
}
100+
```
101+
102+
With it enabled, the comment line is included in alignment:
103+
104+
```powershell
105+
$hashtable = @{
106+
property = 'value'
107+
anotherProperty <#A Comment#> = 'another value'
108+
anotherDifferentProperty = 'yet another value'
109+
}
110+
```
111+
112+
#### CheckEnum: bool (Default value is `$true`)
113+
114+
Enforce alignment of assignment statements of an Enum definition.
115+
116+
#### AlignEnumMemberWithInterveningComment: bool (Default value is `$true`)
117+
118+
Include enum members in the alignment that have an intervening comment - that is to say a comment
119+
between the member name and the equals sign.
120+
121+
Consider the following:
122+
123+
```powershell
124+
enum Enum {
125+
member = 1
126+
anotherMember <#A Comment#> = 2
127+
anotherDifferentMember = 3
128+
}
129+
```
130+
131+
With this setting disabled, the line with the comment is ignored, and it would be aligned like so:
132+
133+
```powershell
134+
enum Enum {
135+
member = 1
136+
anotherMember <#A Comment#> = 2
137+
anotherDifferentMember = 3
138+
}
139+
```
140+
141+
With it enabled, the comment line is included in alignment:
142+
143+
```powershell
144+
enum Enum {
145+
member = 1
146+
anotherMember <#A Comment#> = 2
147+
anotherDifferentMember = 3
148+
}
149+
```
150+
151+
#### IncludeValuelessEnumMembers: bool (Default value is `$true`)
152+
153+
Include enum members in the alignment that don't have an explicitly assigned value. Enum's don't
154+
need to be given a value when they're defined.
155+
156+
Consider the following:
157+
158+
```powershell
159+
enum Enum {
160+
member = 1
161+
anotherMember = 2
162+
anotherDifferentMember
163+
}
164+
```
165+
166+
With this setting disabled, the third line, which has no value, isn't considered when choosing where
167+
to align assignments. It would be aligned like so:
168+
169+
```powershell
170+
enum Enum {
171+
member = 1
172+
anotherMember = 2
173+
anotherDifferentMember
174+
}
175+
```
176+
177+
With it enabled, the valueless member is included in alignment as if it had a value:
178+
179+
```powershell
180+
enum Enum {
181+
member = 1
182+
anotherMember = 2
183+
anotherDifferentMember
184+
}
185+
```

reference/docs-conceptual/PSScriptAnalyzer/Rules/AvoidLongLines.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Avoid long lines
3-
ms.date: 04/29/2025
3+
ms.date: 03/20/2026
44
ms.topic: reference
55
title: AvoidLongLines
66
---
@@ -10,8 +10,8 @@ title: AvoidLongLines
1010

1111
## Description
1212

13-
The length of lines, including leading spaces (indentation), should less than the configured number
14-
of characters. The default length is 120 characters.
13+
The length of lines, including leading spaces (indentation), should be less than the configured
14+
number of characters. The default length is 120 characters.
1515

1616
> [!NOTE]
1717
> This rule isn't enabled by default. The user needs to enable it through settings.

reference/docs-conceptual/PSScriptAnalyzer/Rules/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: List of PSScriptAnalyzer rules
3-
ms.date: 03/27/2024
3+
ms.date: 03/20/2026
44
ms.topic: reference
55
title: List of PSScriptAnalyzer rules
66
---
@@ -68,14 +68,18 @@ The PSScriptAnalyzer contains the following rule definitions.
6868
| [UseCompatibleSyntax](./UseCompatibleSyntax.md) | Warning | No | Yes |
6969
| [UseCompatibleTypes](./UseCompatibleTypes.md) | Warning | No | Yes |
7070
| [UseConsistentIndentation](./UseConsistentIndentation.md) | Warning | No | Yes |
71+
| [UseConsistentParameterSetName](./UseConsistentParameterSetName.md) | Warning | No | |
72+
| [UseConsistentParametersKind](./UseConsistentParametersKind.md) | Warning | No | Yes |
7173
| [UseConsistentWhitespace](./UseConsistentWhitespace.md) | Warning | No | Yes |
74+
| [UseConstrainedLanguageMode](./UseConstrainedLanguageMode.md) | Warning | No | Yes |
7275
| [UseCorrectCasing](./UseCorrectCasing.md) | Information | No | Yes |
7376
| [UseDeclaredVarsMoreThanAssignments](./UseDeclaredVarsMoreThanAssignments.md) | Warning | Yes | |
7477
| [UseLiteralInitializerForHashtable](./UseLiteralInitializerForHashtable.md) | Warning | Yes | |
7578
| [UseOutputTypeCorrectly](./UseOutputTypeCorrectly.md) | Information | Yes | |
7679
| [UseProcessBlockForPipelineCommand](./UseProcessBlockForPipelineCommand.md) | Warning | Yes | |
7780
| [UsePSCredentialType](./UsePSCredentialType.md) | Warning | Yes | |
7881
| [UseShouldProcessForStateChangingFunctions](./UseShouldProcessForStateChangingFunctions.md) | Warning | Yes | |
82+
| [UseSingleValueFromPipelineParameter](./UseSingleValueFromPipelineParameter.md) | Warning | No | |
7983
| [UseSingularNouns](./UseSingularNouns.md) | Warning | Yes | Yes |
8084
| [UseSupportsShouldProcess](./UseSupportsShouldProcess.md) | Warning | Yes | |
8185
| [UseToExportFieldsInManifest](./UseToExportFieldsInManifest.md) | Warning | Yes | |
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
description: Use consistent parameter set names and proper parameter set configuration.
3+
ms.date: 03/20/2026
4+
ms.topic: reference
5+
title: UseConsistentParameterSetName
6+
---
7+
8+
# UseConsistentParameterSetName
9+
10+
**Severity Level: Warning**
11+
12+
## Description
13+
14+
Parameter set names in PowerShell are case-sensitive, unlike most other PowerShell elements. This
15+
rule ensures consistent casing and proper configuration of parameter sets to avoid runtime errors
16+
and improve code clarity.
17+
18+
The rule performs five different checks:
19+
20+
1. **Missing DefaultParameterSetName** - Warns when parameter sets are used but no default is
21+
specified
22+
1. **Multiple parameter declarations** - Detects when a parameter is declared multiple times in the
23+
same parameter set. This is ultimately a runtime exception - this check helps catch it sooner.
24+
1. **Case mismatch between DefaultParameterSetName and ParameterSetName** - Ensures consistent
25+
casing
26+
1. **Case mismatch between different ParameterSetName values** - Ensures all references to the same
27+
parameter set use identical casing
28+
1. **Parameter set names containing newlines** - Warns against using newline characters in parameter
29+
set names
30+
31+
> [!NOTE]
32+
> This rule isn't enabled by default. The user needs to enable it through settings.
33+
34+
## How
35+
36+
- Use a `DefaultParameterSetName` when defining multiple parameter sets
37+
- Ensure consistent casing between `DefaultParameterSetName` and `ParameterSetName` values
38+
- Use identical casing for all references to the same parameter set name
39+
- Avoid declaring the same parameter multiple times in a single parameter set
40+
- Do not use newline characters in parameter set names
41+
42+
## Example
43+
44+
### Wrong
45+
46+
```powershell
47+
# Missing DefaultParameterSetName
48+
function Get-Data {
49+
[CmdletBinding()]
50+
param(
51+
[Parameter(ParameterSetName='ByName')]
52+
[string]$Name,
53+
54+
[Parameter(ParameterSetName='ByID')]
55+
[int]$ID
56+
)
57+
}
58+
59+
# Case mismatch between DefaultParameterSetName and ParameterSetName
60+
function Get-Data {
61+
[CmdletBinding(DefaultParameterSetName='ByName')]
62+
param(
63+
[Parameter(ParameterSetName='byname')]
64+
[string]$Name,
65+
66+
[Parameter(ParameterSetName='ByID')]
67+
[int]$ID
68+
)
69+
}
70+
71+
# Inconsistent casing between ParameterSetName values
72+
function Get-Data {
73+
[CmdletBinding(DefaultParameterSetName='ByName')]
74+
param(
75+
[Parameter(ParameterSetName='ByName')]
76+
[string]$Name,
77+
78+
[Parameter(ParameterSetName='byname')]
79+
[string]$DisplayName
80+
)
81+
}
82+
83+
# Multiple parameter declarations in same set
84+
function Get-Data {
85+
param(
86+
[Parameter(ParameterSetName='ByName')]
87+
[Parameter(ParameterSetName='ByName')]
88+
[string]$Name
89+
)
90+
}
91+
92+
# Parameter set name with newline
93+
function Get-Data {
94+
param(
95+
[Parameter(ParameterSetName="Set`nOne")]
96+
[string]$Name
97+
)
98+
}
99+
```
100+
101+
### Correct
102+
103+
```powershell
104+
# Proper parameter set configuration
105+
function Get-Data {
106+
[CmdletBinding(DefaultParameterSetName='ByName')]
107+
param(
108+
[Parameter(ParameterSetName='ByName', Mandatory)]
109+
[string]$Name,
110+
111+
[Parameter(ParameterSetName='ByName')]
112+
[Parameter(ParameterSetName='ByID')]
113+
[string]$ComputerName,
114+
115+
[Parameter(ParameterSetName='ByID', Mandatory)]
116+
[int]$ID
117+
)
118+
}
119+
```
120+
121+
## Configuration
122+
123+
```powershell
124+
Rules = @{
125+
PSUseConsistentParameterSetName = @{
126+
Enable = $true
127+
}
128+
}
129+
```
130+
131+
### Parameters
132+
133+
- `Enable`: **bool** (Default value is `$false`)
134+
135+
Enable or disable the rule during ScriptAnalyzer invocation.
136+
137+
## Notes
138+
139+
- Parameter set names are case-sensitive in PowerShell, making this different from most other
140+
PowerShell elements
141+
- The first occurrence of a parameter set name in your code is treated as the canonical casing
142+
- Parameters without `[Parameter()]` attributes are automatically part of all parameter sets
143+
- It's a PowerShell best practice to always specify a `DefaultParameterSetName` when using parameter
144+
sets

0 commit comments

Comments
 (0)