How To Test Variables in PowerShell
Get to testing with the Get-Variable, PS drive and the -eq operator.
- By Adam Bertram
Variables are just about the most ubiquitous element inside any PowerShell script. Variables can easily be created and referenced inside your script. But sometimes you just need to test to see if they are available or not. Luckily, PowerShell gives us a few different ways to make that happen. Today, we'll go over three of those ways. We'll cover the Get-Variable cmdlet, the Variable: PS drive and finally I'll show you how to use the -eq operator to tell if the variable has been created.
Get-Variable is a cmdlet that allows you to find all of the variables that have been created in the current console. Let's say I've created a variable called $Variable and I want to use Get-Variable to verify it was created in my console. To do this, I could use the -Name parameter to specify the variable name (without the dollar sign).
You can see that the variable exists and it's an array since there's multiple items inside of the value column. What would happen if $CarAttributes didn't exist? You would receive an error.
Maybe you just want a simple True/False if it exists or not? I could use an if/then construct.
Notice that I have an extra parameter on there called ErrorAction. I'm passing the SilentlyContinue argument to this parameter because, by default, Get-Variable will show that error we received earlier. When I'm simply testing to ensure the variable exists I don't care about this error. Setting ErrorAction to SilentlyContinue silences that error if the variable doesn't exist.
The Variable: PS Drive
PowerShell has a slick way of representing things like the file system, registry, certificate stores, variables, etc. known as PS drives. PS drives allow you to browse different repositories just like you would the file system. One of those PS drives is the Variable PS drive. The Variable PS drive contains all of the variables that are currently stored in memory. To access the Variable PS drive you'd use a similar syntax as you would with the file system by specifying Variable:\.
For our purposes, we just want to see if a variable exists or not. This is what Test-Path is used for all the time for files and folders. I wonder if it will also work for the Variable drive as well.
You can see it works exactly the same as testing for the existence of a file or folder.
Using the -eq Operator
For the final way to test for the existence of a variable I'll be using PowerShell's -eq operator. When a variable doesn't exist, it technically has a value of $null. $null is an automatic variable that essentially represents "does not exist." Knowing this, I can now test for the existence of a variable just like I would if it had a value. Typically, whenever you assign a variable a value you use the -eq operator and test if the variable matches a value like $Variable -eq 'some string'. However, if want to test if the variable exists at all you can simply replace that value with $null.
In the above example, I had previously assigned a value to $Variable. Since that was done, $Variable was not equal to $null thus I received a False result.
You've now seen three different ways to test for the existence of a variable in your PowerShell scripts. You'll find that, depending on the situation, you'll use all three methods at one point in time although I personally have used Test-Path Variable:\variable the most since it's the most concise but the choice is yours.
Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.