Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions docs/Rules/AlignAssignmentStatement.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: Align assignment statement
ms.date: 06/28/2023
ms.date: 03/20/2026
ms.topic: reference
title: AlignAssignmentStatement
---
Expand All @@ -10,14 +10,13 @@ title: AlignAssignmentStatement

## Description

Consecutive assignment statements are more readable when they're aligned.
Assignments are considered aligned when their `equals` signs line up vertically.
Consecutive assignment statements are more readable when they're aligned. Assignments are considered
aligned when their `equals` signs line up vertically.

This rule looks at the key-value pairs in hashtables (including DSC
configurations) as well as enum definitions.
This rule looks at the key-value pairs in hashtables (including DSC configurations) as well as enum
definitions.

Consider the following example which has a hashtable and enum which are not
aligned.
Consider the following example with a hashtable and enum that isn't aligned.

```powershell
$hashtable = @{
Expand Down Expand Up @@ -45,8 +44,8 @@ enum Enum {
}
```

The rule ignores any assignments within hashtables and enums which are on the
same line as others. For example, the rule ignores `$h = @{a = 1; b = 2}`.
The rule ignores any assignments within hashtables and enums which are on the same line as others.
For example, the rule ignores `$h = @{a = 1; b = 2}`.

## Configuration

Expand All @@ -71,15 +70,14 @@ Enable or disable the rule during ScriptAnalyzer invocation.

#### CheckHashtable: bool (Default value is `$true`)

Enforce alignment of assignment statements in a hashtable and in a DSC
Configuration. There is only one setting for hashtable and DSC configuration
because the property value pairs in a DSC configuration are parsed as key-value
pairs of a hashtable.
Enforce alignment of assignment statements in a hashtable and in a DSC Configuration. There is only
one setting for hashtable and DSC configuration because the property value pairs in a DSC
configuration are parsed as key-value pairs of a hashtable.

#### AlignHashtableKvpWithInterveningComment: bool (Default value is `$true`)

Include key-value pairs in the alignment that have an intervening comment - that
is to say a comment between the key name and the equals sign.
Include key-value pairs in the alignment that have an intervening comment - that is to say a comment
between the key name and the equals sign.

Consider the following:

Expand All @@ -91,8 +89,7 @@ $hashtable = @{
}
```

With this setting disabled, the line with the comment is ignored, and it would
be aligned like so:
With this setting disabled, the line with the comment is ignored, and it would be aligned like so:

```powershell
$hashtable = @{
Expand All @@ -118,8 +115,8 @@ Enforce alignment of assignment statements of an Enum definition.

#### AlignEnumMemberWithInterveningComment: bool (Default value is `$true`)

Include enum members in the alignment that have an intervening comment - that
is to say a comment between the member name and the equals sign.
Include enum members in the alignment that have an intervening comment - that is to say a comment
between the member name and the equals sign.

Consider the following:

Expand All @@ -131,8 +128,7 @@ enum Enum {
}
```

With this setting disabled, the line with the comment is ignored, and it would
be aligned like so:
With this setting disabled, the line with the comment is ignored, and it would be aligned like so:

```powershell
enum Enum {
Expand All @@ -154,9 +150,8 @@ enum Enum {

#### IncludeValuelessEnumMembers: bool (Default value is `$true`)

Include enum members in the alignment that don't have an initial value - that
is to say they don't have an equals sign. Enum's don't need to be given a value
when they're defined.
Include enum members in the alignment that don't have an explicitly assigned value. Enums don't
need to be given a value when they're defined.

Consider the following:

Expand All @@ -168,8 +163,8 @@ enum Enum {
}
```

With this setting disabled the third line which has no value is not considered
when choosing where to align assignments. It would be aligned like so:
With this setting disabled, the third line, which has no value, isn't considered when choosing where
to align assignments. It would be aligned like so:

```powershell
enum Enum {
Expand All @@ -179,8 +174,7 @@ enum Enum {
}
```

With it enabled, the valueless member is included in alignment as if it had a
value:
With it enabled, the valueless member is included in alignment as if it had a value:

```powershell
enum Enum {
Expand Down
6 changes: 3 additions & 3 deletions docs/Rules/AvoidLongLines.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: Avoid long lines
ms.date: 04/29/2025
ms.date: 03/20/2026
ms.topic: reference
title: AvoidLongLines
---
Expand All @@ -10,8 +10,8 @@ title: AvoidLongLines

## Description

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

> [!NOTE]
> This rule isn't enabled by default. The user needs to enable it through settings.
Expand Down
2 changes: 1 addition & 1 deletion docs/Rules/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: List of PSScriptAnalyzer rules
ms.date: 03/27/2024
ms.date: 03/20/2026
ms.topic: reference
title: List of PSScriptAnalyzer rules
---
Expand Down
41 changes: 25 additions & 16 deletions docs/Rules/UseConsistentParameterSetName.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: Use consistent parameter set names and proper parameter set configuration.
ms.date: 08/19/2025
ms.date: 03/20/2026
ms.topic: reference
title: UseConsistentParameterSetName
---
Expand All @@ -11,18 +11,25 @@ title: UseConsistentParameterSetName

## Description

Parameter set names in PowerShell are case-sensitive, unlike most other PowerShell elements. This rule ensures consistent casing and proper configuration of parameter sets to avoid runtime errors and improve code clarity.
Parameter set names in PowerShell are case-sensitive, unlike most other PowerShell elements. This
rule ensures consistent casing and proper configuration of parameter sets to avoid runtime errors
and improve code clarity.

The rule performs five different checks:

1. **Missing DefaultParameterSetName** - Warns when parameter sets are used but no default is specified
2. **Multiple parameter declarations** - Detects when a parameter is declared multiple times in the same parameter set. This is ultimately a runtime exception - this check helps catch it sooner.
3. **Case mismatch between DefaultParameterSetName and ParameterSetName** - Ensures consistent casing
4. **Case mismatch between different ParameterSetName values** - Ensures all references to the same parameter set use identical casing
5. **Parameter set names containing newlines** - Warns against using newline characters in parameter set names
1. **Missing DefaultParameterSetName** - Warns when parameter sets are used but no default is
specified
1. **Multiple parameter declarations** - Detects when a parameter is declared multiple times in the
same parameter set. This is ultimately a runtime exception - this check helps catch it sooner.
1. **Case mismatch between DefaultParameterSetName and ParameterSetName** - Ensures consistent
casing
1. **Case mismatch between different ParameterSetName values** - Ensures all references to the same
parameter set use identical casing
1. **Parameter set names containing newlines** - Warns against using newline characters in parameter
set names

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

## How

Expand All @@ -43,7 +50,7 @@ function Get-Data {
param(
[Parameter(ParameterSetName='ByName')]
[string]$Name,

[Parameter(ParameterSetName='ByID')]
[int]$ID
)
Expand All @@ -55,7 +62,7 @@ function Get-Data {
param(
[Parameter(ParameterSetName='byname')]
[string]$Name,

[Parameter(ParameterSetName='ByID')]
[int]$ID
)
Expand All @@ -67,7 +74,7 @@ function Get-Data {
param(
[Parameter(ParameterSetName='ByName')]
[string]$Name,

[Parameter(ParameterSetName='byname')]
[string]$DisplayName
)
Expand Down Expand Up @@ -100,11 +107,11 @@ function Get-Data {
param(
[Parameter(ParameterSetName='ByName', Mandatory)]
[string]$Name,

[Parameter(ParameterSetName='ByName')]
[Parameter(ParameterSetName='ByID')]
[string]$ComputerName,

[Parameter(ParameterSetName='ByID', Mandatory)]
[int]$ID
)
Expand All @@ -129,7 +136,9 @@ Rules = @{

## Notes

- Parameter set names are case-sensitive in PowerShell, making this different from most other PowerShell elements
- Parameter set names are case-sensitive in PowerShell, making this different from most other
PowerShell elements
- 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 DefaultParameterSetName when using parameter sets
- Parameters without `[Parameter()]` attributes are automatically part of all parameter sets
- It's a PowerShell best practice to always specify a `DefaultParameterSetName` when using parameter
sets
54 changes: 33 additions & 21 deletions docs/Rules/UseConsistentParametersKind.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
---
description: Use the same pattern when defining parameters.
ms.date: 03/20/2026
ms.topic: reference
title: UseConsistentParametersKind
---
# UseConsistentParametersKind

**Severity Level: Warning**

## Description

All functions should have same parameters definition kind specified in the rule.
Possible kinds are:
1. `Inline`, i.e.:
```PowerShell
function f([Parameter()]$FirstParam) {
return
}
```
2. `ParamBlock`, i.e.:
```PowerShell
function f {
param([Parameter()]$FirstParam)
return
}
```
All functions should use the same pattern when defining parameters. Possible pattern types are:

* For information: in simple scenarios both function definitions above may be considered as equal. Using this rule as-is is more for consistent code-style than functional, but it can be useful in combination with other rules.
1. `Inline`

```powershell
function f([Parameter()]$FirstParam) {
return
}
```

1. `ParamBlock`

```powershell
function f {
param([Parameter()]$FirstParam)
return
}
```

In simple scenarios, both function definitions shown are considered to be equal. The purpose of this
rule is to enforce consistent code style across the codebase.

## How to Fix

Rewrite function so it defines parameters as specified in the rule

## Example

### When the rule sets parameters definition kind to 'Inline':
```PowerShell
When the rule sets parameters definition kind to `Inline`:

```powershell
# Correct
function f([Parameter()]$FirstParam) {
return
Expand All @@ -42,9 +53,10 @@ function g {
}
```

### When the rule sets parameters definition kind to 'ParamBlock':
```PowerShell
# Inorrect
When the rule sets parameters definition kind to `ParamBlock`:

```powershell
# Incorrect
function f([Parameter()]$FirstParam) {
return
}
Expand Down
Loading
Loading