Skip to content

no-automatic-variable-assignment

PowerShellcorrectnessno fixdefault: warnpowershell/psscriptanalyzer

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.

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.

Reported
...
$script:count = 1
$input = 1
$true = 0
$count = 1
...
$null -eq $x
...
$x -eq $nullable
$null = 1
$global:LASTEXITCODE = 1
foreach ($Event in 1) { }
function Show-It($Error, [string]$Credential) { }
powershell:
lint:
rules:
no-automatic-variable-assignment: warn # off, warn (default), error

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

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.