no-automatic-variable-assignment
What it does
Section titled “What it does”Reports an assignment to an automatic variable, a parameter or a foreach variable that has the name of one ($Error, $Event, $input, $PSItem, $_, $PWD, $Host, $this, $true and so on). $null = ... is not reported.
Why it matters
Section titled “Why it matters”Automatic variables are set by PowerShell, so an assignment is overwritten or is refused, and a parameter with the same name hides the value that other code expects ($input, $Host). The code reads as if it set something that it does not.
To fix, use another name.
Example
Section titled “Example”...$script:count = 1$input = 1$true = 0$count = 1...$null -eq $x...$x -eq $nullable$null = 1$global:LASTEXITCODE = 1foreach ($Event in 1) { }function Show-It($Error, [string]$Credential) { }Configuration
Section titled “Configuration”powershell: lint: rules: no-automatic-variable-assignment: warn # off, warn (default), errorEnabled by the presets: powershell/psscriptanalyzer.
Suppress one line with sudsy-disable-next-line no-automatic-variable-assignment in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.
| Tool | Rule | Match | What differs |
|---|---|---|---|
| psscriptanalyzer | PSAvoidAssignmentToAutomaticVariable |
partial | The report is at the start of the parameter (its attributes included), the module’s at the variable, so a parameter with attributes on earlier lines differs in the line. |