PowerShell global variables are an essential aspect of scripting in Windows PowerShell. They offer a way to store and manipulate data across functions, scripts, and cmdlets. In this tutorial, I will explain to you how to define, set, and use global variable in PowerShell.
To define a global variable in PowerShell, use the Global: scope modifier when declaring the variable, such as $Global:MyGlobalVar = “Value”. This allows the variable to be accessed and modified from anywhere in the script or even in other scripts running in the same PowerShell session. Global variables are useful for sharing data across different parts of a script or among multiple scripts.
PowerShell Global variables are defined with a specific scope that makes them accessible throughout the entire environment, including within functions and modules. By using the global scope, variables can be efficiently shared and reused across different components.
What is a Global Variable in PowerShell?
In PowerShell, a variable is a unit of data storage that you can retrieve and manipulate within your scripts. A global variable, as the name suggests, is accessible from any part of the script, regardless of where it is defined. This is different from local variables, which are accessible only within the scope they are declared, like within a function.
The primary characteristic of a global variable is its scope. It can be accessed and modified from any part of the script or even outside the script if it’s running in the same PowerShell session.
Define a global variable in PowerShell
To declare a global variable in PowerShell, use the $Global: scope modifier followed by the variable name. This ensures that the variable is accessible throughout the entire script and retains its value even after the script’s execution. Here’s an example of declaring a global variable in PowerShell:
$Global:MyVariable = "This is a global variable"
When using PowerShell global variables, it is essential to establish a consistent naming convention. This ensures that your code is more readable and maintainable while minimizing the risk of name clashes between different variables or code components. One widely recommended practice is to use a prefix for global variables. This could be a short string like “g_” or “Global”:
$g_VariableName = "Value"
$Global_VariableName = "Value"
This approach clarifies that these variables are global in scope and helps developers identify them easily within the code.
Access PowerShell global variable
You can access global variables in your PowerShell script using the same $Global: scope modifier. Accessing a global variable is as straightforward as using its name with the scope modifier. Here’s an example of using the global variable declared earlier:
Write-Host $Global:MyVariable
If you’re working with functions, you might need to declare your global variables outside of the functions to ensure they are accessible by other parts of the script. For instance:
$Global:MyVariable = "This is a global variable"
function Show-GlobalVariable {
Write-Host $Global:MyVariable
}
Show-GlobalVariable
Here is the output in the screenshot below after I ran the code using Visual Studio Code.
PowerShell set global variable
Now, let us see how to set a global variable in PowerShell.
Setting a global variable can be done in two ways: by defining a new global variable above or by modifying an existing global variable. Here is how to declare a new global variable:
$Global:MyGlobalVar = "Hello, PowerShell!"
To modify an existing global variable in PowerShell, you simply assign a new value to it using the Global: scope modifier, like the below:
$Global:MyGlobalVar = "New Value"
PowerShell global variable in function
Global variables can be particularly useful when you need to access or modify a variable across different functions.
To use a global variable in a function in PowerShell, first define the variable outside the function using the Global: scope modifier, like $Global:MyVar = “some value”. Inside the function, refer to the variable with the same Global: prefix to access or modify it.
Here’s an example of how to use a global variable within a function in PowerShell:
$Global:MyVar = "Hello, World!"
function Test-Function {
$Global:MyVar = "Updated in Function"
Write-Host "Inside function: $Global:MyVar"
}
Write-Host "Before function: $Global:MyVar"
Test-Function
Write-Host "After function: $Global:MyVar"
In this example, MyVar is a global variable that’s accessed and modified inside Test-Function. The changes made within the function are reflected globally, as indicated by the output before and after the function call.
You can check the screenshot below for the output:
PowerShell global variable example
Now, let us see a simple example to demonstrate how to define, use, and modify a global variable in PowerShell.
First, define a global variable using the Global: scope modifier. This can be done outside of any function.
$Global:myGlobalVar = "Initial Value"
Next, you can create a function that accesses or modifies this global variable.
function Update-GlobalVar {
$Global:myGlobalVar = "Updated in Function"
}
You can now demonstrate how the global variable retains its value across different scopes.
# Display the initial value of the global variable
Write-Host "Global variable before function call: $Global:myGlobalVar"
# Call the function that modifies the global variable
Update-GlobalVar
# Display the updated value of the global variable
Write-Host "Global variable after function call: $Global:myGlobalVar"
Here is the output you can see in the below screenshot.
Conclusion
Global variables in PowerShell provide a convenient way to store and access data across different parts of your script and even across different scripts if they run in the same session. Overuse or improper management of global variables can lead to code that is hard to debug and maintain. Consider if a global variable is the best solution for your needs or if a local variable or a different approach would be more appropriate.
In this PowerShell tutorial, I have explained:
- What is a global variable in PowerShell?
- How to define a PowerShell global variable?
- What to set PowerApps global variable?
- How to use PowerShell global variable in function?
- PowerShell global variable example
You may also like:
- PowerShell check if file modified in last 24 hours
- PowerShell create folder if not exists
- PowerShell get file size
- PowerShell create file if not exists
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com
Hey Bijay this helped me alot but I wanted to show you and anyone else something as the default answer on Google. If you don’t want to set things explicetly ‘everywhere’, you can set it in the function access it from $global.
Eg:
$Changed = $False;
Write-Host “Changed is Defaulted to:” $Changed
function calculateValue ()
{
$global:Changed = $True;
}
Write-Host “Changed is unaffected by a function that has not been called yet:” $Changed
$Changed = $False;
Write-Host “Changed is now:” $Changed”. After it was set OUTSIDE a function”
calculateValue;
Write-Host “Changed is now:” $Changed”. After it was set INSIDE a function as global”
$Changed = $False;
Write-Host “Changed is now:” $Changed”. After it was set OUTSIDE a function again”