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 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:

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:

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:
- How to Check if a Variable is Null in PowerShell?
- Escape Special Characters in Variables in PowerShell
- Share Variables Between Functions in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.