Skip to main content

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/PSAvoidGlobalVars is a PSScriptAnalyzer diagnostic emitted by tally for PowerShell snippets embedded in Dockerfiles.
PropertyValue
SeverityWarning
CategoryPSScriptAnalyzer
Auto-fixNo

Description

A variable is a unit of memory in which values are stored. PowerShell controls access to variables, functions, aliases, and drives through a mechanism known as scoping. Variables and functions that are present when PowerShell starts have been created in the global scope. Globally scoped variables include:
  • Automatic variables
  • Preference variables
  • Variables, aliases, and functions that are in your PowerShell profiles
To understand more about scoping, see Get-Help about_Scopes.

How

Use other scope modifiers for variables.

Examples

Problematic code

$Global:var1 = $null
function Test-NotGlobal ($var)
{
    $a = $var + $var1
}

Correct code

$var1 = $null
function Test-NotGlobal ($var1, $var2)
{
    $a = $var1 + $var2
}

Source

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