How to use global variable in PowerShell inside function?

How to use powershell global variable inside function?

Here we will discuss:
What are PowerShell variable?
How we can declare and use a variable in PowerShell?
How to clear value from a variable and how to delete a variable in PowerShell?
What are global variables in PowerShell?
How we can use global variable in PowerShell inside a function?

What are PowerShell variable?

PowerShell variables are objects which we create and then preseve output, which can be used later. We can store all types of values in PowerShell variables like: ntegers, strings, arrays, hash tables, and objects that represent processes, services, event logs, and computers etc.

How we can declare and use a variable in PowerShell?

We can declare a variable in PowerShell with initial character as “$” and then then name. The name can be alphanumeric characters or even we can use underscore (_) in name.

Variable names are not case-sensitive. Variable names can include spaces and special characters but we should avoid these kind of variable names. Instead we should use _ to create a human readable variable name.

You can read: Windows PowerShell ISE Tutorial

Example:
$TodayDate




Now below way we can use to preserve values:
$TodayDate=(Get-Date).tostring(“dd-MM-yyyy”)
Write-Host $TodayDate

The above statement will write todays date in dd-mm-yyyy format into the host.

How to clear value from a variable and how to delete a variable in PowerShell?

We can clear value from a powershell variable by using Clear-Variable cmdlet like below:

Clear-Variable -name $TodayDate

Or we can also clear value from a variable by assigning $null like below:

$TodayDate = $null

We can delete a variable by using Remove-Variable cmdlets like below:

Remove-Variable -name TodayDate

Also read SharePoint PowerShell article: Debugging PowerShell with Visual Studio Code

What are global variables in PowerShell?

Global variables are variables which can be accessible any where. Ideally we declare global variabes at the begning.

How we can use global variable in PowerShell inside a function?

In PowerShell we can not just declare the variable at the top and can not access from every where. For example I have declared a variable as $message at the open.

Then I had assign 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 were not available.

$message=””

Function MyMethod ($hello)
{
$message+=” xxxxxx”
}
$message+=”dddd ”
$message+=”yyyyy ”

MyMethod -hello “yes hello”

Write-Host $message

See the output.

PowerShell global variable value not accessible outside function
PowerShell global variable value not accessible outside function

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+=” xxxxxx”
}
$global:message+=”dddd ”
$global:message+=”yyyyy ”

MyMethod -hello “yes hello”

Write-Host $global:message

How to use powershell global variable inside function?
How to use powershell global variable inside function?

Hope this article will be helpful.

Similar SharePoint 2013 Tutorials


About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →