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: Avoid exclaim operator
ms.date: 03/26/2024
ms.date: 05/28/2026
ms.topic: reference
title: AvoidExclaimOperator
---
Expand All @@ -10,28 +10,32 @@ title: AvoidExclaimOperator

## Description

Avoid using the negation operator (`!`). Use `-not` for improved readability.
This rule detects the use of the negation operator (an exclamation mark, `!`) and recommends using
the `-not` operator instead for improved readability and consistency with PowerShell conventions.

> [!NOTE]
> This rule is not enabled by default. The user needs to enable it through settings.
The `-not` operator is more explicit and aligns with PowerShell's verbose style, making code
easier to understand at a glance.

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

## Example

### Wrong
### Noncompliant

```powershell
$MyVar = !$true
```

### Correct
### Compliant

```powershell
$MyVar = -not $true
```

## Configuration
## Configure rule

To enable this rule, run the following command:

```powershell
Rules = @{
Expand All @@ -41,8 +45,9 @@ Rules = @{
}
```

### Parameters
## Parameters

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

Enable or disable the rule during ScriptAnalyzer invocation.
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,6 +1,6 @@
---
description: Avoid global aliases.
ms.date: 06/28/2023
description: Avoid global aliases
ms.date: 05/28/2026
ms.topic: reference
title: AvoidGlobalAliases
---
Expand All @@ -10,28 +10,31 @@ title: AvoidGlobalAliases

## Description

Globally scoped aliases override existing aliases within the sessions with matching names. This name
collision can cause difficult to debug issues for consumers of modules and scripts.
This rule detects the use of the `New-Alias` command to create aliases in the global scope. Global
aliases can unintentionally override existing aliases in the session, leading to unexpected behavior
and name collisions. Name collisions make it difficult for module consumers to diagnose issues and
maintain code reliability.

To understand more about scoping, see `Get-Help about_Scopes`.

**NOTE** This rule is not available in PowerShell version 3 or 4 because it uses the
This rule is not available in PowerShell version 3 or 4 because it uses the
`StaticParameterBinder.BindCommand` API.

## How

Use other scope modifiers for new aliases.
To avoid this issue, define aliases without the **Scope** parameter, or use other appropriate scope
modifiers. To learn more, see [about_Scopes][01].

## Example

### Wrong
### Noncompliant

```powershell
New-Alias -Name Name -Value Value -Scope Global
```

### Correct
### Compliant

```powershell
New-Alias -Name Name1 -Value Value
```

<!-- link references -->

[01]: /powershell/module/microsoft.powershell.core/about/about_scopes
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@ title: AvoidGlobalFunctions

## Description

Globally scoped functions override existing functions within the sessions with matching names. This
name collision can cause difficult to debug issues for consumers of modules.
This rule detects function definitions that use the `global:` scope modifier on the function name to
define a function in the global scope. Global functions can unintentionally override existing
functions in the session, leading to unexpected behavior and name collisions. Name collisions make
it difficult for module consumers to diagnose issues and maintain code reliability.

To understand more about scoping, see `Get-Help about_Scopes`.

## How

Use other scope modifiers for functions.
To avoid this issue, define functions without the global scope modifier, or use other appropriate
scope modifiers. To learn more, see [about_Scopes][01].

## Example

### Wrong
### Noncompliant

```powershell
function global:functionName {}
```

### Correct
### Compliant

```powershell
function functionName {}
```

<!-- link references -->

[01]: /powershell/module/microsoft.powershell.core/about/about_scopes
31 changes: 20 additions & 11 deletions reference/docs-conceptual/PSScriptAnalyzer/Rules/AvoidGlobalVars.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: No Global Variables
ms.date: 06/28/2023
description: Avoid global variables
ms.date: 05/28/2026
ms.topic: reference
title: AvoidGlobalVars
---
Expand All @@ -10,25 +10,28 @@ title: AvoidGlobalVars

## Description

A variable is a unit of memory in which values are stored. PowerShell controls access to variables,
functions, aliases, and drives through a mechanism known as scoping. Variables and functions that
are present when PowerShell starts have been created in the global scope.
This rule detects usage of variables with the global scope modifier. PowerShell controls access to
variables, functions, aliases, and drives through a mechanism known as scoping. Variables and
functions that are present when PowerShell starts have been created in the global scope.

Globally scoped variables include:

- Automatic variables
- Preference variables
- Variables, aliases, and functions that are in your PowerShell profiles

To understand more about scoping, see `Get-Help about_Scopes`.
This rule doesn't detect use of the `New-Variable` cmdlet to create variables in the global scope or
the other `*-Variable` cmdlets to work with variables in the global scope. It only detects variable
expressions with the global scope modifier, like `$global:example`.

## How
This rule doesn't apply to variable expressions that use the global scope modifier where the
variable name is for an [automatic variable][01] or a [preference variable][02].

Use other scope modifiers for variables.
Use local or script scope for variables instead of the global scope. To learn more, see
[about_Scopes][03].

## Example

### Wrong
### Noncompliant

```powershell
$Global:var1 = $null
Expand All @@ -38,7 +41,7 @@ function Test-NotGlobal ($var)
}
```

### Correct
### Compliant

```powershell
$var1 = $null
Expand All @@ -47,3 +50,9 @@ function Test-NotGlobal ($var1, $var2)
$a = $var1 + $var2
}
```

<!-- 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: Avoid Invoking Empty Members
ms.date: 06/28/2023
description: Avoid invoking empty members
ms.date: 05/28/2026
ms.topic: reference
title: AvoidInvokingEmptyMembers
---
Expand All @@ -10,25 +10,29 @@ title: AvoidInvokingEmptyMembers

## Description

Invoking non-constant members can cause potential bugs. Please double check the syntax to make sure
that invoked members are constants.
This rule detects expressions where the code uses the [member-access operator][01] (`.`) where the
member name is dynamically constructed. Invoking dynamically constructed member names can introduce
unexpected behavior and runtime errors. Ensure that member invocations use constant, literal member
names rather than expressions that are evaluated at runtime.

## How

Provide the requested members for a given type or class.
Replace dynamic member name expressions with constant member names for the target type or class.

## Example

### Wrong
### Noncompliant

```powershell
$MyString = 'abc'
$MyString.('len'+'gth')
```

### Correct
### Compliant

```powershell
$MyString = 'abc'
$MyString.('length')
```

<!-- link references -->

[01]: /powershell/module/microsoft.powershell.core/about/about_operators#member-access-operator-
40 changes: 28 additions & 12 deletions reference/docs-conceptual/PSScriptAnalyzer/Rules/AvoidLongLines.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: Avoid long lines
ms.date: 03/20/2026
ms.date: 06/01/2026
ms.topic: reference
title: AvoidLongLines
---
Expand All @@ -10,29 +10,45 @@ 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.
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.

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

## Configuration
### Noncompliant

```
In this example of sample text, the maximum line length is using the default setting of not exceeding more than 120
characters.
```

### Compliant

```
In this example of sample text, the maximum line length is set to not exceed
more than 80 characters.
```

## Configure rule

```powershell
Rules = @{
PSAvoidLongLines = @{
Enable = $true
PSAvoidLongLines = @{
Enable = $true
MaximumLineLength = 120
}
}
```

## Parameters

### `Enable`: bool (Default value is `$false`)
### Enable

Enable or disable the rule during ScriptAnalyzer invocation.
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`.

### `MaximumLineLength`: int (Default value is 120)
### MaximumLineLength

Optional parameter to override the default maximum line length.
This parameter is optional and defines the maximum length for a line before it violates the rule. It
accepts an integer value. The default value is `120`.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: Avoid multiple type specifiers on parameters.
ms.date: 06/28/2023
description: Avoid multiple type specifiers on parameters
ms.date: 06/01/2026
ms.topic: reference
title: AvoidMultipleTypeAttributes
---
Expand All @@ -10,16 +10,16 @@ title: AvoidMultipleTypeAttributes

## Description

Parameters should not have more than one type specifier. Multiple type specifiers on parameters
can cause runtime errors.
This rule detects parameters that have multiple type specifiers applied to them. Parameters
shouldn't have multiple type specifiers. When you apply more than one type attribute to a parameter,
it can lead to unexpected type coercion or runtime errors.

## How

Ensure each parameter has only 1 type specifier.
Each parameter should have exactly one type specifier to ensure predictable behavior and type
safety.

## Example

### Wrong
### Noncompliant

```powershell
function Test-Script
Expand All @@ -34,7 +34,7 @@ function Test-Script
}
```

### Correct
### Compliant

```powershell
function Test-Script
Expand Down
Loading
Loading