In PowerShell, how do I test whether or not a specific variable exists in global scope?

Powershell

Powershell Problem Overview


I'm using PowerShell scripts for some UI automation of a WPF application. Normally, the scripts are run as a group, based on the value of a global variable. It's a little inconvenient to set this variable manually for the times when I want to run just one script, so I'm looking for a way to modify them to check for this variable and set it if not found.

test-path variable:\foo doesn't seem to work, since I still get the following error:

> The variable '$global:foo' cannot be retrieved because it has not been set.

Powershell Solutions


Solution 1 - Powershell

Test-Path can be used with a special syntax:

Test-Path variable:global:foo

This also works for environment variables ($env:foo):

Test-Path env:foo

And for non-global variables (just $foo inline):

Test-Path variable:foo

Solution 2 - Powershell

EDIT: Use stej's answer below. My own (partially incorrect) one is still reproduced here for reference:


You can use

Get-Variable foo -Scope Global

and trap the error that is raised when the variable doesn't exist.

Solution 3 - Powershell

Personal preference is to use Ignore over SilentlyContinue here because it's not an error at all. Since we're expecting it to potentially be $false let's prevent it (with Ignore) from being put (albeit silently) in the $Error stack.

You can use:

if (Get-Variable 'foo' -Scope 'Global' -ErrorAction 'Ignore') {
    $true
} else {
    $false
}

More tersely:

[bool](gv foo -s global -ea ig)

Output of either:

False

Alternatively

You can trap the error that is raised when the variable doesn't exist.

try {
    Get-Variable foo -Scope Global -ErrorAction 'Stop'
} catch [System.Management.Automation.ItemNotFoundException] {
    Write-Warning $_
}

Outputs:

WARNING: Cannot find a variable with the name 'foo'.

Solution 4 - Powershell

You can assign a variable to the return value of Get-Variable then check to see if it is null:

$variable = Get-Variable -Name foo -Scope Global -ErrorAction SilentlyContinue

if ($variable -eq $null)
{
    Write-Host "foo does not exist"
}

# else...

Just be aware that the variable has to be assigned to something for it to "exist". For example:

$global:foo = $null

$variable = Get-Variable -Name foo -Scope Global -ErrorAction SilentlyContinue

if ($variable -eq $null)
{
    Write-Host "foo does not exist"
}
else
{
    Write-Host "foo exists"
}

$global:bar

$variable = Get-Variable -Name bar -Scope Global -ErrorAction SilentlyContinue

if ($variable -eq $null)
{
    Write-Host "bar does not exist"
}
else
{
    Write-Host "bar exists"
}

Output:

foo exists
bar does not exist

Solution 5 - Powershell

Simple:

 [boolean](get-variable "Varname" -ErrorAction SilentlyContinue)

Solution 6 - Powershell

So far, it looks like the answer that works is this one.

To break it out further, what worked for me was this:

Get-Variable -Name foo -Scope Global -ea SilentlyContinue | out-null

$? returns either true or false.

Solution 7 - Powershell

$myvar = if ($env:variable) { $env:variable } else { "default_value" } 

Solution 8 - Powershell

You can try:

$global:foo -eq $null

This will return $true if the $global:foo is not set.

Solution 9 - Powershell

Test the existence of variavle MyVariable. Returns boolean true or false.

Test-Path variable:\MyVariable

Solution 10 - Powershell

There's an even easier way:

if ($variable)
{
    Write-Host "bar exist"
}
else
{
    Write-Host "bar does not exists"
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScott LawrenceView Question on Stackoverflow
Solution 1 - PowershellstejView Answer on Stackoverflow
Solution 2 - PowershellJoeyView Answer on Stackoverflow
Solution 3 - PowershellVertigoRayView Answer on Stackoverflow
Solution 4 - PowershellGeorge HowarthView Answer on Stackoverflow
Solution 5 - PowershellKeesGView Answer on Stackoverflow
Solution 6 - PowershellScott LawrenceView Answer on Stackoverflow
Solution 7 - PowershellShirish ShuklaView Answer on Stackoverflow
Solution 8 - Powershellbus1heroView Answer on Stackoverflow
Solution 9 - PowershelldegavrocheView Answer on Stackoverflow
Solution 10 - PowershellWortheboyView Answer on Stackoverflow