PowerShell How-To

How To Test Variables in PowerShell

Get to testing with the Get-Variable, PS drive and the -eq operator.

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
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).

[Click on image for larger view.]  Figure 1.

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.

[Click on image for larger view.]  Figure 2.

Maybe you just want a simple True/False if it exists or not? I could use an if/then construct.

[Click on image for larger view.]  Figure 3.

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.

Figure 4.

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.

Figure 5.

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.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular