> ## Documentation Index
> Fetch the complete documentation index at: https://tally.wharflab.com/llms.txt
> Use this file to discover all available pages before exploring further.

# powershell/PSAvoidAssignmentToAutomaticVariable

> Changing automatic variables might have undesired side effects

`powershell/PSAvoidAssignmentToAutomaticVariable` is a PSScriptAnalyzer diagnostic emitted by tally for PowerShell snippets embedded in Dockerfiles.

| Property | Value            |
| -------- | ---------------- |
| Severity | Warning          |
| Category | PSScriptAnalyzer |
| Auto-fix | No               |

## 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 a value on those. Other automatic variables should
only be assigned in certain special cases to achieve a certain effect as a special technique.

To understand more about automatic variables, see `Get-Help about_Automatic_Variables`.

## How

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

## Examples

### Problematic code

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 theme={null}
function foo($Error){ }
```

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

### Correct code

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

## Source

This rule documentation is adapted from Microsoft's PSScriptAnalyzer documentation for
[AvoidAssignmentToAutomaticVariable](https://github.com/MicrosoftDocs/PowerShell-Docs-Modules/blob/main/reference/docs-conceptual/PSScriptAnalyzer/Rules/AvoidAssignmentToAutomaticVariable.md),
licensed under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).
