description | ms.custom | ms.date | ms.topic | title |
---|---|---|---|---|
Extra Variables |
PSSA v1.21.0 |
06/28/2023 |
reference |
UseDeclaredVarsMoreThanAssignments |
Severity Level: Warning
Variables that are assigned but not used are not needed.
Note
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'.
Remove the variables that are declared but not used.
function Test
{
$declaredVar = 'Declared just for fun'
$declaredVar2 = 'Not used'
Write-Output $declaredVar
}
function Test
{
$declaredVar = 'Declared just for fun'
Write-Output $declaredVar
}
The following example triggers the PSUseDeclaredVarsMoreThanAssignments warning because $bar
is not used within the scriptblock where it was defined.
$foo | ForEach-Object {
if ($_ -eq $false) {
$bar = $true
}
}
if($bar){
Write-Host 'Collection contained a false case.'
}