CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Automatic Variables – $TRUE, $FALSE and $null

We have discussed a few automatic variables in our previous Article “PowerShell – Automatic Variables – $$, $^, $? and $_“. In this article, we will discuss a few more automatic variables.

The $TRUE and $FALSE automatic variables

These variables represent the values True and False respectively. When you compare the result of the conditional expression or when you compare the result of the last command execution, it is good to use these variables; instead of the text “True” or “False”. Here are some examples:

PS C:\> $? -EQ "True"
False
PS C:\> $? -EQ "False"
True
PS C:\> $True
True
PS C:\> $TRUE
True
PS C:\> $FALSE
False
PS C:\> $?
True
PS C:\> $? -EQ $FALSE
False
PS C:\> $? -EQ $TRUE
True
PS C:\> $TRUE -EQ $FALSE
False

The $null automatic variable

$null automatic variable contains the NULL value. We can use this to represent an absent or undefined value. For example, if the variable is declared and NOT assigned to any value; PowerShell treats it as a NULL value. We can use this variable during comparison to verify whether the defined variable has any value assigned.

Note that, Empty value is NOT a NULL value. Here is the examples:

PS C:\> $v
PS C:\> $v -EQ $null
True
PS C:\> $v = ""
PS C:\> $v -EQ $null
False
PS C:\>

We discuss more as we go.

[..] David

PowerShell – Automatic Variables – $TRUE, $FALSE and $null

Leave a Reply

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

Scroll to top