Home » PowerShell » PowerShell Global Variables

PowerShell Global Variables

PowerShell global variables are essential in scripts, functions, or modules. They can store data that can be accessible across scripts, and code.

Use the $global: scope modifier followed by a variable name to create a global variable name. You can also use the Set-Variable cmdlet to assign value to a variable in the same statement.

In this article, we will discuss how to create, and initialize a global variable, and access and modify the global variables in PowerShell.

Declare and Initialize Global Variable in PowerShell

To create and initialize a global variable in PowerShell, use the $global: scope modifier followed by the variable name.

# Declare and Initialize a global variable
$global:piValue = 3.145

The output of the above PowerShell script creates a global variable having a value of 3.145 assigned to it.

You can use the Set-Variable cmdlet to create a variable and assign a value to the variable in the same statement.

# Using Set-Variable cmdlet
Set-Variable -Name piValue -Value 3.145 -Scope Global

The output of the above PowerShell scripts creates a variable having global scope and assigned the value of 3.145 to it.

Accessing and Modifying Global Variables in Functions

You can access and modify the global variable value by using $global: scope modifier in a function.

# Define a function that accesses and modifies a global variable
function Update-InterestRate{
    $global:interestRate = 2
}

# Call the function
Update-GlobalVar

# Check the value of the global variable
Write-Output $global:interestRate 

In the above PowerShell script, the Update-InterestRate function uses the $global: scope modifier to update the variable value to a new value.

Creating Read-Only PowerShell Global Variable

The Set-Variable cmdlet in PowerShell has a -Option parameter to specify access modifiers like read-only, constants, etc…

Set-Variable -Name piValue -Value 3.145 -Scope Global -Option ReadOnly

In the above PowerShell script, the Set-Variable cmdlet uses the -Name parameter to specify the variable name and -Scope parameter for global, and the -Option parameter to specify a read-only variable.

The output of the above PowerShell script creates a read-only global variable and if you want to assign a new value, it will throw an exception.

PS C:\> Set-Variable -Name piValue -Value 3.145 -Scope Global -Option ReadOnly                                          PS C:\> $piValue                                                                                                        

3.145
PS C:\> $piValue = 3.12                                                                                                 Cannot overwrite variable piValue because it is read-only or constant.
At line:1 char:1
+ $piValue = 3.12
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (piValue:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

PS C:\>        

Best Practices for Using Global Variables in PowerShell

Limit the use of global variables as it makes your script harder to understand and maintain. Consider using the local variables when possible.

Use a constant naming convention for global variables to easily identify them and document global variables to understand their purpose and usage.

Conclusion

I hope the above article on PowerShell global variables is helpful to you.

By using the global variables judiciously and following best practices, it will help you maintain the script.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.