How to Check if a Variable Exists in PowerShell

To check if a variable exists in PowerShell, test its path on the Variable: drive. Test-Path returns True when the variable is defined, even if it holds $null, and False when it was never created:

Test-Path -Path variable:city

Get-Variable with -ErrorAction SilentlyContinue does the same job. The one check that doesn’t work is $city -eq $null, because a missing variable and an empty one look identical to it.

In this tutorial, I’ll show you how to check if a variable exists in PowerShell in scripts, functions and strict mode, plus environment variables and parameters.

Check if a variable exists with Get-Variable

Get-Variable returns the variable object when it exists and writes an error when it doesn’t. Silence that error and the result works directly in an if statement:

$city = 'Austin'

if (Get-Variable -Name city -ErrorAction SilentlyContinue) { 'city exists' }
if (-not (Get-Variable -Name state -ErrorAction SilentlyContinue)) { 'state does not exist' }

try {
    Get-Variable -Name state -ErrorAction Stop
}
catch {
    "Failed. $($_.Exception.Message)"
}
"Value only: $(Get-Variable -Name city -ValueOnly)"

Output:

city exists
state does not exist
Failed. Cannot find a variable with the name 'state'.
Value only: Austin

Use -ErrorAction Stop with try/catch when a missing variable should stop the script. Note the name goes in without the $ sign, the same way New-Variable and Set-Variable take names.

-ValueOnly returns just the value, which helps when the variable name is built at run time, such as "server$i".

Test-Path variable: is the shortest check

PowerShell exposes every variable on a drive called Variable:, so the same Test-Path you use for checking files and folders works on variable names:

$city = 'Austin'

"city:  $(Test-Path -Path variable:city)"
"state: $(Test-Path -Path variable:state)"
"cit*:  $(Test-Path -Path variable:cit*)"

Output:

city:  True
state: False
cit*:  True

There’s one trap in that output: cit* returned True. Test-Path treats * ? and [ ] as wildcards, so it only answers “does anything match?” for those names. Use Get-Variable -Name with exact names when you build them dynamically.

Is a $null variable the same as one that doesn’t exist?

No. A variable can exist and still hold $null, either because you assigned $null or because you emptied it with Clear-Variable. Here, budget was never created at all:

$manager = $null
$region = 'East'
Clear-Variable -Name region

'{0,-9} {1,-8} {2}' -f 'Name', 'Exists', 'IsNull'
foreach ($name in 'manager', 'region', 'budget') {
    $exists = Test-Path -Path "variable:$name"
    $isNull = $null -eq (Get-Variable -Name $name -ValueOnly -ErrorAction Ignore)
    '{0,-9} {1,-8} {2}' -f $name, $exists, $isNull
}

Output:

Name      Exists   IsNull
manager   True     True
region    True     True
budget    False    True
PowerShell Test-Path variable shows which variables exist versus hold null
Exists comes from Test-Path, IsNull from a $null check (PowerShell 7)

All three are null, but only budget doesn’t exist. Clear-Variable keeps the variable and empties it, while Remove-Variable deletes it, so Test-Path returns False afterward.

If you really want to know whether a string has text, that’s a diff