Skip to content
Draft
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 (`!`) 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,12 @@ Rules = @{
}
```

### Parameters
## Parameters

### Enable

Enables (`$true`) the rule during ScriptAnalyzer invocation.

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

Enable or disable the rule during ScriptAnalyzer invocation.
Disables (`$false`) the rule during ScriptAnalyzer invocation. 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
ms.date: 05/28/2026
ms.topic: reference
title: AvoidGlobalAliases
---
Expand All @@ -10,28 +10,29 @@ 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.
Global aliases can override existing aliases in the current session. This override
creates naming conflicts that lead to hard to diagnose problems for module and script consumers.

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.
Instead of using the Global scope, use other scope modifiers such as `Local` or `Process` when
creating new aliases. 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,27 @@ 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.
Global functions can unintentionally override existing functions in the session, leading to
unexpected behavior and name collisions. Name collisions makes 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: No Global Variables
ms.date: 06/28/2023
ms.date: 05/28/2026
ms.topic: reference
title: AvoidGlobalVars
---
Expand All @@ -20,15 +20,12 @@ Globally scoped variables include:
- Preference variables
- Variables, aliases, and functions that are in your PowerShell profiles

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

## How

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

## Example

### Wrong
### Noncompliant

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

### Correct
### Compliant

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

<!-- link references -->

[01]: /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
ms.date: 05/28/2026
ms.topic: reference
title: AvoidInvokingEmptyMembers
---
Expand All @@ -10,23 +10,22 @@ title: AvoidInvokingEmptyMembers

## Description

Invoking non-constant members can cause potential bugs. Please double check the syntax to make sure
that invoked members are constants.
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'
Expand Down
2 changes: 2 additions & 0 deletions reference/docs-conceptual/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ items:
href: PSScriptAnalyzer/Rules/AvoidDefaultValueForMandatoryParameter.md
- name: AvoidDefaultValueSwitchParameter
href: PSScriptAnalyzer/Rules/AvoidDefaultValueSwitchParameter.md
- name: AvoidExclaimOperator
href: PSScriptAnalyzer/Rules/AvoidExclaimOperator.md
- name: AvoidGlobalAliases
href: PSScriptAnalyzer/Rules/AvoidGlobalAliases.md
- name: AvoidGlobalFunctions
Expand Down