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,13 +1,15 @@
---
description: Align assignment statement
ms.date: 06/12/2026
ms.date: 07/21/2026
ms.topic: reference
title: AlignAssignmentStatement
---
# AlignAssignmentStatement

**Severity Level: Warning**

**Default state: Disabled**

## Description

This rule detects misaligned assignment operators in hashtables and enum definitions. Consecutive
Expand Down Expand Up @@ -51,7 +53,7 @@ enum Enum {
}
```

## Configuration
## Configure rule

```powershell
Rules = @{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
---
description: Changing automatic variables might have undesired side effects
ms.date: 06/12/2026
ms.date: 07/21/2026
ms.topic: reference
title: AvoidAssignmentToAutomaticVariable
---
# AvoidAssignmentToAutomaticVariable

**Severity Level: Warning**

**Default state: Always enabled**

## Description

This rule detects assignments to automatic variables and parameter names that use automatic variable
names. PowerShell automatically defines variables that store internal state information and manages
them on its own. Even though you _can_ override many automatic variables, doing so can have
unexpected effects for users and make your code harder to maintain and debug.
Avoid using automatic variable names in your functions and parameters. This rule detects assignments
to automatic variables and parameter names that use automatic variable names. PowerShell
automatically defines variables that store internal state information and manages them on its own.
Even though you _can_ override many automatic variables, doing so can have unexpected effects for
users and make your code harder to maintain and debug.

Avoid using automatic variable names in your functions and parameters. Reserve automatic variables
for PowerShell's internal use only, and rely on them only to read state information.
Reserve automatic variables for PowerShell's internal use only, and rely on them only to read state
information.

To learn more, see [about_Automatic_Variables][01].

<!-- TODO
Ability to suppress was added in https://github.com/PowerShell/PSScriptAnalyzer/pull/1896
Need documentation for how to configure suppression of this rule.
-->

## How

Use variable names in functions or their parameters that do not conflict with automatic variables.

## Example

### Noncompliant
Expand All @@ -50,6 +44,16 @@ function Get-CustomErrorMessage($ErrorMessage){ $Error = "Error occurred: $Error
function Get-CustomErrorMessage($ErrorMessage){ $FinalErrorMessage = "Error occurred: $ErrorMessage" }
```

<!-- Link reference definitions -->
## Configure rule

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][02].

<!-- Link references -->
[01]: /powershell/module/microsoft.powershell.core/about/about_automatic_variables
[02]: ../using-scriptanalyzer.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
---
description: Avoid Default Value For Mandatory Parameter
ms.date: 06/12/2026
ms.date: 07/21/2026
ms.topic: reference
title: AvoidDefaultValueForMandatoryParameter
---
# AvoidDefaultValueForMandatoryParameter

**Severity Level: Warning**

**Default state: Always enabled**

## Description

This rule detects when mandatory parameters have default values assigned. Mandatory parameters
Expand Down Expand Up @@ -45,3 +47,16 @@ function Test
)
}
```

## Configure rule

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][02].

<!-- Link references -->
[02]: ../using-scriptanalyzer.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
---
description: Switch parameters should not default to $true
ms.date: 06/12/2026
ms.date: 07/21/2026
ms.topic: reference
title: AvoidDefaultValueSwitchParameter
---
# AvoidDefaultValueSwitchParameter

**Severity Level: Warning**

**Default state: Always enabled**

## Description

This rule detects switch parameters that are assigned a default value of `$true`. Switch parameters
Expand Down Expand Up @@ -64,6 +66,17 @@ function Test-Script
}
```

## Configure rule

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][02].

<!-- link references -->

[01]: /powershell/scripting/developer/cmdlet/strongly-encouraged-development-guidelines#parameters-that-take-true-and-false
[02]: ../using-scriptanalyzer.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
---
description: Avoid exclaim operator
ms.date: 05/28/2026
ms.date: 07/21/2026
ms.topic: reference
title: AvoidExclaimOperator
---
# AvoidExclaimOperator

**Severity Level: Warning**

**Default state: Disabled**

## Description

This rule detects the use of the negation operator (an exclamation mark, `!`) and recommends using
Expand All @@ -16,9 +18,6 @@ the `-not` operator instead for improved readability and consistency with PowerS
The `-not` operator is more explicit and aligns with PowerShell's verbose style, making code
easier to understand at a glance.

This rule is **disabled** by default. Enable it explicitly during ScriptAnalyzer invocation if
desired.

## Example

### Noncompliant
Expand All @@ -33,12 +32,7 @@ $MyVar = !$true
$MyVar = -not $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`.
## Configure rule

```powershell
Rules = @{
Expand All @@ -47,3 +41,10 @@ Rules = @{
}
}
```

## 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`.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
---
description: Avoid global aliases.
ms.date: 05/28/2026
ms.date: 07/21/2026
ms.topic: reference
title: AvoidGlobalAliases
---
# AvoidGlobalAliases

**Severity Level: Warning**

**Default state: Always enabled**

## Description

This rule detects the use of the `New-Alias` command to create aliases in the global scope. Global
Expand All @@ -32,6 +34,16 @@ New-Alias -Name Name -Value Value -Scope Global
New-Alias -Name Name1 -Value Value
```

<!-- link references -->
## Configure rule

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][02].

<!-- link references -->
[01]: /powershell/module/microsoft.powershell.core/about/about_scopes
[02]: ../using-scriptanalyzer.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
---
description: Avoid global functions and aliases
ms.date: 06/28/2023
ms.date: 07/21/2026
ms.topic: reference
title: AvoidGlobalFunctions
---
# AvoidGlobalFunctions

**Severity Level: Warning**

**Default state: Always enabled**

## Description

This rule detects function definitions that use the `global:` scope modifier on the function name to
Expand All @@ -32,5 +34,16 @@ function global:functionName {}
function functionName {}
```

## Configure rule

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][02].

<!-- link references -->
[01]: /powershell/module/microsoft.powershell.core/about/about_scopes
[02]: ../using-scriptanalyzer.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
---
description: Avoid global variables.
ms.date: 06/23/2026
ms.date: 07/21/2026
ms.topic: reference
title: AvoidGlobalVars
---
# AvoidGlobalVars

**Severity Level: Warning**

**Default state: Always enabled**

## Description

You should avoid modifying global variables in your scripts and functions because other scripts or
Expand Down Expand Up @@ -56,7 +58,18 @@ Test-NotGlobal 'bar'
$var1
```

## Configure rule

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][04].

<!-- link references -->
[01]: /powershell/module/microsoft.powershell.core/about/about_automatic_variables
[02]: /powershell/module/microsoft.powershell.core/about/about_preference_variables
[03]: /powershell/module/microsoft.powershell.core/about/about_scopes
[04]: ../using-scriptanalyzer.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
---
description: Avoid invoking empty members.
ms.date: 05/28/2026
ms.date: 07/21/2026
ms.topic: reference
title: AvoidInvokingEmptyMembers
---
# AvoidInvokingEmptyMembers

**Severity Level: Warning**

**Default state: Always enabled**

## Description

This rule detects expressions where the code uses the [member-access operator][01] (`.`) where the
Expand All @@ -33,5 +35,16 @@ $MyString = 'abc'
$MyString.('length')
```

## Configure rule

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][02].

<!-- link references -->
[01]: /powershell/module/microsoft.powershell.core/about/about_operators#member-access-operator-
[02]: ../using-scriptanalyzer.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
---
description: Avoid long lines
ms.date: 06/01/2026
ms.date: 07/21/2026
ms.topic: reference
title: AvoidLongLines
---
# AvoidLongLines

**Severity Level: Warning**

**Default state: Disabled**

## Description

This rule detects lines that exceed the configured maximum length, including leading spaces
(indentation). The default maximum line length is 120 characters. This rule is **disabled** by
default and must be explicitly enabled through rule configuration settings.
(indentation). The default maximum line length is 120 characters.

## Example

Expand Down
Loading
Loading