Edit

AvoidAssignmentToAutomaticVariable

Severity Level: Warning

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

How

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

Example

Noncompliant

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.

function foo($Error){ }
function Get-CustomErrorMessage($ErrorMessage){ $Error = "Error occurred: $ErrorMessage" }

Compliant

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