Skip to content
Merged
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
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,38 +10,54 @@ title: AlignAssignmentStatement

## Description

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

The rule looks for key (property) value pairs in a hashtable (DSC configuration) to check if they
are aligned or not. Consider the following example in which the key value pairs are not aligned.
This rule looks at the key-value pairs in hashtables (including DSC configurations) as well as enum
definitions.

Consider the following example with a hashtable and enum that isn't aligned.

```powershell
$hashtable = @{
property1 = 'value'
property = 'value'
anotherProperty = 'another value'
}

enum Enum {
member = 1
anotherMember = 2
}
```

Alignment in this case would look like the following.

```powershell
$hashtable = @{
property1 = 'value'
property = 'value'
anotherProperty = 'another value'
}

enum Enum {
member = 1
anotherMember = 2
}
```

The rule ignores hashtables in which the assignment statements are on the same line. 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

```powershell
Rules = @{
PSAlignAssignmentStatement = @{
Enable = $true
CheckHashtable = $true
Enable = $true
CheckHashtable = $true
AlignHashtableKvpWithInterveningComment = $true
CheckEnum = $true
AlignEnumMemberWithInterveningComment = $true
IncludeValuelessEnumMembers = $true
}
}
```
Expand All @@ -52,8 +68,118 @@ Rules = @{

Enable or disable the rule during ScriptAnalyzer invocation.

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

Enforce alignment of assignment statements in a hashtable and in a DSC Configuration. There is only
one switch for hasthable and DSC configuration because the property value pairs in a DSC
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.

Consider the following:

```powershell
$hashtable = @{
property = 'value'
anotherProperty <#A Comment#> = 'another value'
anotherDifferentProperty = 'yet another value'
}
```

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

```powershell
$hashtable = @{
property = 'value'
anotherProperty <#A Comment#> = 'another value'
anotherDifferentProperty = 'yet another value'
}
```

With it enabled, the comment line is included in alignment:

```powershell
$hashtable = @{
property = 'value'
anotherProperty <#A Comment#> = 'another value'
anotherDifferentProperty = 'yet another value'
}
```

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

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.

Consider the following:

```powershell
enum Enum {
member = 1
anotherMember <#A Comment#> = 2
anotherDifferentMember = 3
}
```

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

```powershell
enum Enum {
member = 1
anotherMember <#A Comment#> = 2
anotherDifferentMember = 3
}
```

With it enabled, the comment line is included in alignment:

```powershell
enum Enum {
member = 1
anotherMember <#A Comment#> = 2
anotherDifferentMember = 3
}
```

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

Include enum members in the alignment that don't have an explicitly assigned value. Enum's don't
need to be given a value when they're defined.

Consider the following:

```powershell
enum Enum {
member = 1
anotherMember = 2
anotherDifferentMember
}
```

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 {
member = 1
anotherMember = 2
anotherDifferentMember
}
```

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

```powershell
enum Enum {
member = 1
anotherMember = 2
anotherDifferentMember
}
```
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 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
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 Expand Up @@ -68,14 +68,18 @@ The PSScriptAnalyzer contains the following rule definitions.
| [UseCompatibleSyntax](./UseCompatibleSyntax.md) | Warning | No | Yes |
| [UseCompatibleTypes](./UseCompatibleTypes.md) | Warning | No | Yes |
| [UseConsistentIndentation](./UseConsistentIndentation.md) | Warning | No | Yes |
| [UseConsistentParameterSetName](./UseConsistentParameterSetName.md) | Warning | No | |
| [UseConsistentParametersKind](./UseConsistentParametersKind.md) | Warning | No | Yes |
| [UseConsistentWhitespace](./UseConsistentWhitespace.md) | Warning | No | Yes |
| [UseConstrainedLanguageMode](./UseConstrainedLanguageMode.md) | Warning | No | Yes |
| [UseCorrectCasing](./UseCorrectCasing.md) | Information | No | Yes |
| [UseDeclaredVarsMoreThanAssignments](./UseDeclaredVarsMoreThanAssignments.md) | Warning | Yes | |
| [UseLiteralInitializerForHashtable](./UseLiteralInitializerForHashtable.md) | Warning | Yes | |
| [UseOutputTypeCorrectly](./UseOutputTypeCorrectly.md) | Information | Yes | |
| [UseProcessBlockForPipelineCommand](./UseProcessBlockForPipelineCommand.md) | Warning | Yes | |
| [UsePSCredentialType](./UsePSCredentialType.md) | Warning | Yes | |
| [UseShouldProcessForStateChangingFunctions](./UseShouldProcessForStateChangingFunctions.md) | Warning | Yes | |
| [UseSingleValueFromPipelineParameter](./UseSingleValueFromPipelineParameter.md) | Warning | No | |
| [UseSingularNouns](./UseSingularNouns.md) | Warning | Yes | Yes |
| [UseSupportsShouldProcess](./UseSupportsShouldProcess.md) | Warning | Yes | |
| [UseToExportFieldsInManifest](./UseToExportFieldsInManifest.md) | Warning | Yes | |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
---
description: Use consistent parameter set names and proper parameter set configuration.
ms.date: 03/20/2026
ms.topic: reference
title: UseConsistentParameterSetName
---

# UseConsistentParameterSetName

**Severity Level: Warning**

## 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.

The rule performs five different checks:

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 isn't enabled by default. The user needs to enable it through settings.

## How

- Use a `DefaultParameterSetName` when defining multiple parameter sets
- Ensure consistent casing between `DefaultParameterSetName` and `ParameterSetName` values
- Use identical casing for all references to the same parameter set name
- Avoid declaring the same parameter multiple times in a single parameter set
- Do not use newline characters in parameter set names

## Example

### Wrong

```powershell
# 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
)
}
```

### Correct

```powershell
# 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

```powershell
Rules = @{
PSUseConsistentParameterSetName = @{
Enable = $true
}
}
```

### Parameters

- `Enable`: **bool** (Default value is `$false`)

Enable or disable the rule during ScriptAnalyzer invocation.

## Notes

- 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
Loading