Sync docs updates to source repo by sdwheeler · Pull Request #2190 · PowerShell/PSScriptAnalyzer · GitHub
Skip to content
Closed
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
101 changes: 41 additions & 60 deletions docs/Rules/AlignAssignmentStatement.md
24 changes: 16 additions & 8 deletions docs/Rules/AvoidAssignmentToAutomaticVariable.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: Changing automatic variables might have undesired side effects
ms.date: 06/28/2023
ms.date: 06/12/2026
ms.topic: reference
title: AvoidAssignmentToAutomaticVariable
---
Expand All @@ -10,11 +10,15 @@ title: AvoidAssignmentToAutomaticVariable

## Description

PowerShell has built-in variables known as automatic variables. Many of them are read-only and
PowerShell throws an error when trying to assign an value on those. Other automatic variables should
only be assigned in certain special cases to achieve a certain effect as a special technique.
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.

To understand more about automatic variables, see `Get-Help about_Automatic_Variables`.
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.

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

<!-- TODO
Ability to suppress was added in https://github.com/PowerShell/PSScriptAnalyzer/pull/1896
Expand All @@ -27,9 +31,9 @@ Use variable names in functions or their parameters that do not conflict with au

## Example

### Wrong
### Noncompliant

The variable `$Error` is an automatic variables that exists in the global scope and should therefore
The variable `$Error` is an automatic variable that exists in the global scope and should therefore
never be used as a variable or parameter name.

```powershell
Expand All @@ -40,8 +44,12 @@ function foo($Error){ }
function Get-CustomErrorMessage($ErrorMessage){ $Error = "Error occurred: $ErrorMessage" }
```

### Correct
### Compliant

```powershell
function Get-CustomErrorMessage($ErrorMessage){ $FinalErrorMessage = "Error occurred: $ErrorMessage" }
```

<!-- Link reference definitions -->

[01]: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_automatic_variables
13 changes: 7 additions & 6 deletions docs/Rules/AvoidDefaultValueForMandatoryParameter.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: Avoid Default Value For Mandatory Parameter
ms.date: 06/28/2023
ms.date: 06/12/2026
ms.topic: reference
title: AvoidDefaultValueForMandatoryParameter
---
Expand All @@ -10,13 +10,14 @@ title: AvoidDefaultValueForMandatoryParameter

## Description

Mandatory parameters should not have a default values because there is no scenario where the default
can be used. PowerShell prompts for a value if the parameter value is not specified when calling the
function.
This rule detects when mandatory parameters have default values assigned. Mandatory parameters
shouldn't have default values because they can't be used. When a parameter is marked as mandatory,
PowerShell always prompts the user for a value if one isn't supplied when calling the function. Any
default value assigned to a mandatory parameter is unreachable and serves no purpose.

## Example

### Wrong
### Noncompliant

```powershell
function Test
Expand All @@ -31,7 +32,7 @@ function Test
}
```

### Correct
### Compliant

```powershell
function Test
Expand Down
40 changes: 15 additions & 25 deletions docs/Rules/AvoidDefaultValueSwitchParameter.md
Loading
Loading