Skip to main content
powershell/PSUseDeclaredVarsMoreThanAssignments is a PSScriptAnalyzer diagnostic emitted by tally for PowerShell snippets embedded in Dockerfiles.

Description

Variables that are assigned but not used are not needed.
For this rule, the variable must be used within the same scriptblock that it was declared or it won’t be considered to be ‘used’.

How

Remove the variables that are declared but not used.

Examples

Problematic code

Correct code

Special cases

The following examples trigger the PSUseDeclaredVarsMoreThanAssignments warning. This behavior is a limitation of the rule. There is no way to avoid these false positive warnings. In this case, the warning is triggered because $bar is not used within the scriptblock where it was defined.
In the next example, the warning is triggered because $errResult isn’t recognized as being used in the Write-Host command.

Source

This rule documentation is adapted from Microsoft’s PSScriptAnalyzer documentation for UseDeclaredVarsMoreThanAssignments, licensed under CC BY 4.0.