How to create and use PowerShell global variable

In this PowerShell tutorial, we are going to learn PowerShell global variable:

  • What is a PowerShell global variable?
  • How to create or declare a PowerShell global variable?
  • How to use PowerShell global variable inside a function?
  • How to set a PowerShell global variable from a function?
  • How to set a PowerShell global variable from a function by passing the global variable as a parameter to the function.
  • How to use the PowerShell Set-Variable Cmdlets to set value to a PowerShell global variable as well as to a local variable and PowerShell variable scoping.

If you are new to PowerShell, before reading PowerShell global variable, you should know PowerShell Variables. I have recently written a tutorial on PowerShell variables.

PowerShell global variable – Video Tutorial

Here, is a video tutorial on how to create and use PowerShell global variable.

Subscribe to EnjoySharePoint YouTube channel for more Free SharePoint and related videos.

What is PowerShell global variable?

PowerShell global variables are variable which can be accessed anywhere inside the script, functions, etc. Ideally, we declare a global variable in PowerShell at the beginning.

PowerShell global variable is accessible to scripts, function and to any cmdlet in the current session. The good approach is to declare the global variable at the top with proper syntax.

Note: Only declaring a variable at the top will not become a global variable in PowerShell.

In PowerShell, if a global variable and a local variable has the same name, then the local variable can shadow the PowerShell global variable.

How to declare a PowerShell global variable?

We can declare a PowerShell global variable by using the global keywords like below:

$global:myglobalvariable="This is a PowerShell global variable"
or
$global:myglobalvariable2 = $null

How can we use PowerShell global variable inside a function?

In PowerShell, we can not just declare the variable at the top and can not access from everywhere. For example, I have declared a variable as $message at the top of the PowerShell script.

Then I had assigned some values outside of a function and also I assigned some values inside a function. But when I come out of the function I can see the values which were assigned outside of the function. Whatever assigned inside the function was not available.

$message=""
Function MyMethod ($hello)
{
$message+=$hello
}
MyMethod -hello "Hello "
$message+="Bijay "
$message+="Kumar "
Write-Host $message

I have written the PowerShell script inside Windows PowerShell ISE, you can also use Visual Studio code to write, debug and test PowerShell script.

You can see the output:

powershell global variable
PowerShell global variable

So in PowerShell unless you declare a variable with $global: keyword the variable will not be considered as global by PowerShell. So we have to declare the global variable in PowerShell like below:

$global:message=""

Now check the below script. All the values are captured.

$global:message=""
Function MyMethod ($hello)
{
$global:message+=$hello
}
MyMethod -hello "Hello "
$global:message+="Bijay "
$global:message+="Kumar "
Write-Host $global:message
how to create powershell global variable
How to use powershell global variable inside function?

If you really do not need to use in a global scope, then avoid declaring PowerShell variables as global.

How to set a PowerShell global variable from a function

Now, we will see how we can set a PowerShell global variable from a function. Here I have declared a global variable like:

$global:fullname = $null

And the global variable will populate from the PowerShell function.

$global:fullname = $null

function GetFullName ($firstname, $lastname)
{
   $firstname +"  "+ $lastname
}

$global:fullname = GetFullName Bijay Kumar

Write-Host $global:fullname

Once you run the below script using PowerShell ISE, you can see the output, the PowerShell global variable hold the values set inside the function.

How to set a PowerShell global variable from a function
set a PowerShell global variable from a function

How to set a PowerShell global variable from a function by passing the global variable as a parameter to the function

Now, we will see how to set a PowerShell global variable from a function by passing the global variable as a parameter to the function.

For this kind of requirement, you need to pass the powershell global variable as a reference type like below:

$global:calculatednum =""

function CalCulateValue ($num1, $num2, [REF]$finalnum)
{
    $finalnum.Value = $num1 + $num2
}

#You can then call it like this:
CalCulateValue 5 6 ([REF]$global:calculatednum)

Write-Host $global:calculatednum
set a PowerShell global variable from a function by passing the global variable as a parameter to the function
set a PowerShell global variable from a function by passing the global variable as a parameter to the function

Set PowerShell Global Variable Value using Set-Variable Cmdlets

In PowerShell, we can use Set-Variable cmdlets to set the value of a variable. If the variable has values previously, then the value will be overridden.

Set-Variable cmdlet is used to create the variable in PowerShell, as well as assigning value to the variable.

Set-Variable Cmdlets to Create PowerShell local variable

Below is an example, where we are setting value to the variable and also getting the value from the variable.

Set-Variable -Name "fullname" -Value "Bijay Kumar"
Get-Variable -Name "fullname"

Once you run the PowerShell script, you can see the output like below:

Set PowerShell Global Variable Value using Set-Variable Cmdlets
Set PowerShell Global Variable Value using Set-Variable Cmdlets

Set-Variable Cmdlets to Create PowerShell Global variable

Similarly, to Set PowerShell Global Variable Value using Set-Variable cmdlets, we have to use -Scope global in the Set-Variable cmdlet.

Set-Variable -Name "TodayDate" -Value (Get-Date) -Scope global

Get-Variable -Name "TodayDate"

The above PowerShell script will display value of TodayDate which is set as a global variable.

Set PowerShell Global Variable Value using Set-Variable
Set PowerShell Global Variable Value using Set-Variable

Note: Set-Variable cmdlets variable also create the variable if the PowerShell variabe does not exist.

Here the scope is very important, to set global scope, you have to set the PowerShell scope as Global. Below are 5 PowerShell Scope you can declare.

  • Global
  • Local
  • Script
  • Private
  • Numbered Scopes

You can read more on this MSDN link to know more on PowerShell Scopes.

Set-Variable Cmdlets to Create a Constant/ReadOnly PowerShell Global variable

We can also use the Set-Variable PowerShell cmdlets to create a constant or readonly PowerShell global variable.

Set-Variable -Name "TodayDate" -Value (Get-Date) -Option constant -Scope global 
Set-Variable -Name "TodayDate" -Value (Get-Date) -Option ReadOnly -Scope global 
Set-Variable -Name "TodayDate" -Value (Get-Date) -Option Private -Scope global 

Here you need to set constant/ReadOnly/Private to the -Option parameter in Set-Variable cmdlet.

PowerShell Variable Scoping Rules

PowerShell variable scoping is very important, scopes decides where the variable will be available.

A PowerShell variable is visible in the scope on which it was created and in child scope, unless you declare the varioable as Private or global.

The variable can be modified in the scope where it is created.

To read a variable from a different scope, you ned to prefix scope like

  • $global:myvariable
  • $script:myvariable
  • $private:myvariable

PowerShell global vs local variables

PowerShell local variables are available in the current scope. You can also retrieve all the variables in the local scope by using following cmdlets.
Get-Variable -Scope local

But PowerShell global variables are available in the global scope(scope that is in effect when PowerShell starts). Variables and functions that are present when PowerShell starts have been created in the global scope. Like automatic variables or preference variables etc.

You can get all the variables in the global scope by using the below PowerShell cmdlets.

Get-Variable -Scope global

You may like following PowerShell tutorials:

Conclusion

Here in this PowerShell tutorials, we discussed what is a PowerShell global variable, how to create a PowerShell global variable? And how to use PowerShell global variable.

I also showed how can we use PowerShell global variable inside a function like how to set and get PowerShell global variable value.

We also saw how we to set a PowerShell global variable from a function by passing the global variable as a parameter to the function.

>