An easy way in PowerShell to check if a variable has a value

Checking if a variable has a value is something I do very often in my scripts.
Most of the times, I do this by checking if the variable equals to $null.
However, there is a more performance friendly way to do this…

First, let’s create a few variables, which have the four most common types:

1

Next, let’s verify the types, just to show you that check it in the way I will show you, works for most (if not all) variable types:

2

Now to actually check if the variables have values:

3

…and to show you it correctly tells me if the parameter does exist, yet has no value in it:

4

Leave a Reply

Your email address will not be published. Required fields are marked *