Updated March 13, 2023
Introduction to PowerShell global variable
Variables are an integral part of PowerShell. There are many types of variables; this article will focus on global variables. Global variables are the ones which are available to all scripts, functions, or any cmdlet within the current session. Usually, global variables are declared at the beginning, mostly at the top. The major use of a global variable is its reusability. This article will explain in detail global variables, syntax its use, along with appropriate examples.
Syntax of PowerShell Global variable
The way of creating a global variable is as follows
$global:test="This is a test"
or
$global:test = $null
Example:
Write-Host "Example of global variable in PS" -ForegroundColor Green
$global:test=""
Function test ($tes)
{
$global:test+=$tes
}
test -tes "this "
$global:test+="is "
$global:test+="test "
Write-Host $global:test -ForegroundColor Green
Output:
Example:
Setting a global variable through a function
Write-Host "Demo of calling global variable from function" -ForegroundColor Green
$global:fln = $null
function getfn ($fn, $ln)
{
$fn +" "+ $ln
}
$global:fln = getfn vignesh krishnakumar
Write-Host "the function value is set to global variable" -ForegroundColor Green
write-Host "the value is "$global:fln
$global:fln1 = $null
function getfn1 ($fn1, $ln1, $chn1)
{
$fn1 +" "+ $ln1 +" "+ $chn1
}
Write-Host "Demo of global variable with function as three parameters" -ForegroundColor Green
$global:fln1 = getfn1 vignesh krishnakumar Chennai
Write-Host " the value is " $global:fln1
Output:
Example:
Input:
Write-Host "Demo of passing global variable as a parameter to a function"
Write-Host "global variable needs to be declared as reference"
$global:finalsum =""
function sum ($no1, $no2, [REF]$resr)
{
$resr.Value = $no1 + $no2
}
#You can then call it like this:
sum 51 61 ([REF]$global:finalsum)
Write-Host " the value of the global variable is" $global:finalsum
$global:finalsum1 =""
function sum1 ($no11, $no22, $no33,$no44,[REF]$resr1)
{
$resr1.Value = $no11 + $no22 +$no33 + $no44
}
#You can then call it like this:
sum1 51 61 71 81 ([REF]$global:finalsum1)
Write-Host " the value of the global variable is" $global:finalsum1
Advantages of Global Variable
- It can store all types of data types values such as strings, objects, arrays, integers, and hast tables.
- It can store a collection of different object types
- Every variable is allotted a memory
- If a value is not assigned to a global variable, it is considered empty by default.
- Though it is not mandatory, it is always best to declare the global variables at the top for easy understanding.
Scope Hierarchy
As specified prior, once you dispatch a PowerShell session, PowerShell creates some things for you within the worldwide scope. These things can be capacities, factors assumed names, or PSDrives. Anything you define in your PowerShell session will be defined within the global scope also. Since you’re within the worldwide scope by default, if you are doing something that makes another scope like executing a script or running a workshop, a child scope will be made with the parent being the worldwide scope. Scopes are like forms with parents and children. So anything that’s characterized in a parent scope, the worldwide scope, in this case, will be available within the child’s scope. But these things are as they were editable within the scope they were characterized in.
Different types of scope modifiers:
Global: this denotes the global scope
Local: This denotes the only the current scope
Private: this denotes that the item is private and available within the current scope
Script: this denotes that the item can be accessed throughout the script
Using: This is used to access items from other scripts
Workflow: this denotes items used within a workflow
Using Modifier:
This is used to work with items in a remote cmdlet. Whenever anything that needs to be accessed outside the current session, the using modifier should be used. This is one step higher than the global variable. Some example cmdlets are Start-Job and Invoke-Command.
Example:
Input:
Write-Host "Demo of setting global variable value" -ForegroundColor Green
Set-Variable -Name "TD" -Value (Get-Date) -Scope global
Get-Variable -Name "TD"
Set-Variable -Name "NAME1" -Value "vignesh" -Scope global
Set-Variable -Name "age1" -Value "28" -Scope global
Set-Variable -Name "city" -Value "chennai" -Option Private -Scope global
Set-Variable -Name "status" -Value "married" -Scope global
Set-Variable -Name "gender" -Value "male" -Scope global
Get-Variable -Name "NAME1"
Get-Variable -Name "age1"
Get-Variable -Name "city"
Get-Variable -Name "status"
Get-Variable -Name "gender"
Output:
Example:
Input:
Write-Host "Example of global variable in PS" -ForegroundColor Green
$global:eg=""
Function test ($par)
{
$global:eg+=$par
}
test -par "welcome"
$global:eg += "user "
$global:eg+="mark "
Write-Host $global:eg -ForegroundColor Green
$global:abc = $null
function geabcdtfn ($fn, $ln)
{
$fn +" "+ $ln
}
$global:abc = geabcdtfn vijaya sethupathi
write-Host "the value is "$global:abc
$global:fln1 = $null
function getfn1 ($fn1, $ln1, $chn1)
{
$fn1 +" "+ $ln1 +" "+ $chn1
}
Write-Host "Demo of global variable with function as three parameters" -ForegroundColor Green
$global:fln1 = getfn1 vignesh krishnakumar Chennai
Write-Host " the value is " $global:fln1
Set-Variable -Name "tdate" -Value (Get-Date) -Scope global
Set-Variable -Name "mark1" -Value "100" -Scope global
Set-Variable -Name "mark2" -Value "90" -Scope global
Set-Variable -Name "mark3" -Value "100" -Option Private -Scope global
Set-Variable -Name "mark4" -Value "100" -Scope global
Set-Variable -Name "total" -Value "390" -Scope global
Get-Variable -Name "mark1"
Get-Variable -Name "mark1"
Get-Variable -Name "mark1"
Get-Variable -Name "mark1"
Get-Variable -Name "total"
Get-Variable -Name "tdate"
Output:
Conclusion
Thus, the article explained in detail the global variable in PowerShell. It explained in detail how to define a global variable, set a value to the global variable, pass the global variable to a function, and set the value returned by a function to a global variable. Though global variables are available for the entire session, it is not advisable to create all the variables as global variables, leading to bad practice. To learn more in detail, it is advisable to write sample scripts and practice them.
Recommended Articles
This is a guide to the PowerShell Global variable. Here we discuss how to define a global variable and how to set a value to the variable. You may also have a look at the following articles to learn more –