Boolean Values and Operators

Boolean Values and Operators

  • Comments 2

John Smith asked what TRUE and FALSE were in PowerShell on our newsgroup Microsoft.Public.Windows.PowerShell.   The simple answer to this is $TRUE and $FALSE but the complete answer is a richer.  PowerShell has a very rich notion of TRUE and FALSE.  The best way to explain it is to show it. 

PS>  function test ($VALUE) {
>> if ($VALUE) {
>>     Write-Host -ForegroundColor GREEN "TRUE"
>> } else {
>>     Write-Host -ForegroundColor RED   "FALSE"
>> }
>> }
>>
PS> test $TRUE
TRUE
PS> test $FALSE
FALSE
PS> test TRUE
TRUE
PS> test FALSE
TRUE

The question a lot of PowerShell newbies ask is:  ‘Why is “FALSE” TRUE?”   In PowerShell, Strings can be evaluated as Booleans.  If a string is ZERO length – it is false, otherwise it is TRUE.  “FALSE” has 5 characters so it is TRUE.

PS> test "SHORT STRING"
TRUE
PS> test ""
FALSE
PS> $x=("                    " -replace " ")
PS> test $x
FALSE
PS>

Given that see if you can figure out what is going on with the sequence below:

PS> test "0"
TRUE
PS> test 0
FALSE
PS> test 1
TRUE
PS> test 0.0
FALSE
PS> test 0x0
FALSE
PS> test 0mb
FALSE
PS> test 0kb
FALSE
PS> test 0D
FALSE
PS> test 0.00000001
TRUE
PS>

“0” is TRUE because it is a STRING and it has a length of 1.  0 is FALSE because it is a number and that number is 0.  In PowerShell, any number which evaluates to 0 is FALSE and every non-zero number is TRUE.  The example shows you a floating point zero, a hexadecimal zero, 0 megs, 0 kilos, 0 decimal, there are all sorts of zeros but to PowerShell, they all evaluate to FALSE. 

The next one should be a little easier to figure out.  The last 2 lines shows you what you would have to type if we didn’t have a rich concept of TRUE and FALSE.

PS> $x=@(1,2,3,4,5)
PS> test $x
TRUE
PS> $x=@()
PS> test $x
FALSE
PS> test (get-Process)
TRUE
PS> test (get-Process |where {$_.name -eq "PowerShell"})
TRUE
PS> test (get-Process |where {$_.name -eq "NoSuchProcess"})
FALSE
PS> test (@(get-Process |where {$_.name -eq "PowerShell"}).count -ne 0)
TRUE
PS> test (@(get-Process |where {$_.name -eq "NoSuchProcess"}).count -ne 0)
FALSE

Here is your last example.  If a variable is defined, we use its value to determine TRUE/FALSE but if the variable is not defined – it is FALSE.

PS> $x=10
PS> test $x
TRUE
PS> $x=0
PS> test $x
FALSE
PS> test $NoSuchVariable
FALSE
PS> set-psdebug -strict
PS> $NoSuchVariable
The variable $NoSuchVariable cannot be retrieved because it has not been se
t yet.
At line:1 char:15
+ $NoSuchVariable <<<<

PS>

PowerShell has a rich notion of TRUE/FALSE because it dramatically reduces the overhead and junk that you need to deal with when doing your work.

Happy Holidays!


Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 7 and 7 and type the answer here:
  • Post
  • In my previous post about Boolean Values And Operators I made the following point: " PowerShell has a

  • Your "rich definition" is simply weak typing, and isn't always the greatest thing, especially if you get a type you aren't expecting.

Page 1 of 1 (2 items)