diff --git a/reference/docs-conceptual/PSScriptAnalyzer/Rules/AvoidGlobalVars.md b/reference/docs-conceptual/PSScriptAnalyzer/Rules/AvoidGlobalVars.md index add12c6..ba8413e 100644 --- a/reference/docs-conceptual/PSScriptAnalyzer/Rules/AvoidGlobalVars.md +++ b/reference/docs-conceptual/PSScriptAnalyzer/Rules/AvoidGlobalVars.md @@ -1,6 +1,6 @@ --- description: Avoid global variables -ms.date: 05/28/2026 +ms.date: 06/23/2026 ms.topic: reference title: AvoidGlobalVars --- @@ -10,11 +10,14 @@ title: AvoidGlobalVars ## Description +You should avoid modifying global variables in your scripts and functions because other scripts or +functions that run in the same session can depend on them. This can lead to unexpected behavior and +make it difficult to debug your code. + This rule detects usage of variables with the global scope modifier. 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: +functions that are present when PowerShell starts were created in the global scope. Globally scoped +variables include: - Automatic variables - Preference variables @@ -34,21 +37,23 @@ Use local or script scope for variables instead of the global scope. To learn mo ### Noncompliant ```powershell -$Global:var1 = $null -function Test-NotGlobal ($var) -{ - $a = $var + $var1 +$var1 = 'foo' +function Test-NotGlobal ($var) { + $Global:var1 = $var } +Test-NotGlobal 'bar' +$var1 ``` ### Compliant ```powershell -$var1 = $null -function Test-NotGlobal ($var1, $var2) -{ - $a = $var1 + $var2 +$var1 = 'foo' +function Test-NotGlobal ($var) { + $var1 = $var } +Test-NotGlobal 'bar' +$var1 ``` diff --git a/reference/docs-conceptual/PSScriptAnalyzer/Rules/UseApprovedVerbs.md b/reference/docs-conceptual/PSScriptAnalyzer/Rules/UseApprovedVerbs.md index 436db23..0223627 100644 --- a/reference/docs-conceptual/PSScriptAnalyzer/Rules/UseApprovedVerbs.md +++ b/reference/docs-conceptual/PSScriptAnalyzer/Rules/UseApprovedVerbs.md @@ -25,8 +25,7 @@ To learn more, see [Approved Verbs for PowerShell Commands][01]. ### Noncompliant ```powershell -function Change-Item -{ +function Change-Item { ... } ``` @@ -34,8 +33,7 @@ function Change-Item ### Compliant ```powershell -function Update-Item -{ +function Update-Item { ... } ``` diff --git a/reference/docs-conceptual/PSScriptAnalyzer/create-custom-rule.md b/reference/docs-conceptual/PSScriptAnalyzer/create-custom-rule.md index 1e2a21c..c4a3fea 100644 --- a/reference/docs-conceptual/PSScriptAnalyzer/create-custom-rule.md +++ b/reference/docs-conceptual/PSScriptAnalyzer/create-custom-rule.md @@ -130,7 +130,7 @@ Export-ModuleMember -Function (FunctionName) .DESCRIPTION The #Requires statement prevents a script from running unless the Windows PowerShell version, modules, snap-ins, and module and snap-in version prerequisites are met. - From Windows PowerShell 4.0, the #Requires statement let script developers require that + Since Windows PowerShell 4.0, the #Requires statement lets script developers require that sessions be run with elevated user rights (run as Administrator). Script developers do not need to write their own methods any more. To fix a violation of this rule, please consider using #Requires -RunAsAdministrator instead of your own methods.