Converting VBScript's IsEmpty Function

Definition: Returns a Boolean value indicating whether a variable has been initialized.


Rather than get bogged down in a long discussion on metaphysics, let’s go with a rudimentary explanation of the difference between an empty variable and a null variable. A null variable is a value we know nothing about; we can't even say for certain whether the variable has a value. By contrast, we know the value of an empty variable: it's nothing. An empty variable is a variable that has no value (for example, it's been assigned an empty string as its value). A null variable - well, like we said, we don't know anything at all about a null variable.

So how do we know whether or not a variable is empty? Well, for now about the only approach we’ve come up with is to check and see whether or not the variable has a Length equal to 0. An empty variable has a length equal to 0; a null variable does not. (Because we don't know anything about a null variable, we have no idea what its length might be.)

Confused? We don’t blame you. But maybe this example will help. In our first command, we assign an empty string ("") to the variable $a; in the second command, we use the -eq operator to determine whether or not the Length of $a is equal to 0. That value gets stored in the variable $b:

$a = ""
$b = $a.length -eq 0

When you run this command and then echo back the value of $b you should get the following:


We can’t guarantee that this will always work, but it’s a good place to start.

Return to the VBScript to Windows PowerShell home page