Skip to content

Files

Latest commit

 

History

History
42 lines (29 loc) · 873 Bytes

AvoidGlobalVars.md

File metadata and controls

42 lines (29 loc) · 873 Bytes

Pattern: Use of global variable

Issue: -

Description

Global variables are strongly discouraged as they can cause errors across different systems.

Globally scoped variables include:

  • Automatic variables
  • Preference variables
  • Variables, aliases, and functions that are in your Windows PowerShell profiles

To understand more about scoping, see Get-Help about_Scopes.

How

Use other scope modifiers for variables.

Example of incorrect code:

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

Example of correct code:

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

Further Reading