How to Check if a Variable Exists in PowerShell?

As a PowerShell user, you should find out whether a specific variable exists before using it in your script. In this tutorial, I will show you different methods to check if a variable exists in PowerShell and provide some real examples.

To check if a variable exists in PowerShell, you can use the Get-Variable cmdlet. For example, to check if a variable named $stateName exists, you can use the following code: if (Get-Variable -Name stateName -ErrorAction SilentlyContinue) { Write-Host "The variable 'stateName' exists." } else { Write-Host "The variable 'stateName' does not exist." }.

Method 1: Using the Get-Variable Cmdlet

The best way to check if a variable exists in PowerShell is by using the Get-Variable cmdlet. This cmdlet allows you to retrieve information about variables in the current PowerShell session.

Here’s an example:

$stateName = "California"

if (Get-Variable -Name stateName -ErrorAction SilentlyContinue) {
    Write-Host "The variable 'stateName' exists and its value is: $stateName"
} else {
    Write-Host "The variable 'stateName' does not exist."
}

In this example, we first define a variable $stateName and assign it the value “California”. Then, we use the Get-Variable cmdlet with the -Name parameter to check if the variable exists. By specifying -ErrorAction SilentlyContinue, we suppress any error messages that would occur if the variable doesn’t exist.

You can see the output in the screenshot below after executing the script using VS code.

Check if a Variable Exists in PowerShell

Check out Split a String into Variables in PowerShell

Method 2: Using the Test-Path Cmdlet with the Variable: Provider

Another effective method to check for the existence of a variable is by utilizing the Test-Path cmdlet in combination with the Variable: provider. The Test-Path cmdlet is commonly used to verify the existence of files or directories, but it can also be used with the Variable: provider to check for variables.

Here’s an example:

$zipCode = 90210

if (Test-Path variable:zipCode) {
    Write-Host "The variable 'zipCode' exists and its value is: $zipCode"
} else {
    Write-Host "The variable 'zipCode' does not exist."
}

In this case, we define a variable $zipCode and assign it the value 90210, representing a famous ZIP code in the USA. We then use the Test-Path cmdlet with the variable: provider followed by the variable name. If the variable exists, the condition evaluates to $true, and the corresponding message is displayed.

Here is the output in the screenshot below:

powershell check if variable exists

Read PowerShell Local Variables

Method 3: Using the -eq Operator with $null

A third approach to check if a variable exists in PowerShell is by comparing it to $null using the -eq operator. If a variable hasn’t been defined or assigned a value, it is considered equal to $null.

Here’s an example:

$nationalPark = "Yellowstone"

if ($nationalPark -eq $null) {
    Write-Host "The variable 'nationalPark' does not exist or has no value assigned."
} else {
    Write-Host "The variable 'nationalPark' exists and its value is: $nationalPark"
}

In this example, we define a variable $nationalPark and assign it the value “Yellowstone”, referring to the famous national park in the United States. We then use the -eq operator to compare $nationalPark with $null. If the variable doesn’t exist or has no value assigned, the condition evaluates to $true, and the corresponding message is displayed.

Here is the output in the screenshot below:

how to check if variable exists in PowerShell

Conclusion

In this tutorial, I explained how to check if a variable exists in PowerShell using different methods like using the Get-Variable cmdlet, the Test-Path cmdlet with the Variable: provider, or the -eq operator with $null, etc. I always recommend using the Get-Variable PowerShell cmdlet to check whether the variable exists or not.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.