Automating the world one-liner at a time…
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 $TRUETRUEPS> test $FALSEFALSEPS> test TRUETRUEPS> test FALSETRUE
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"TRUEPS> test ""FALSEPS> $x=(" " -replace " ")PS> test $xFALSEPS>
Given that see if you can figure out what is going on with the sequence below:
PS> test "0"TRUEPS> test 0FALSEPS> test 1TRUEPS> test 0.0FALSEPS> test 0x0FALSEPS> test 0mbFALSEPS> test 0kbFALSEPS> test 0DFALSEPS> test 0.00000001TRUEPS>
“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 $xTRUEPS> $x=@()PS> test $xFALSEPS> test (get-Process)TRUEPS> test (get-Process |where {$_.name -eq "PowerShell"})TRUEPS> test (get-Process |where {$_.name -eq "NoSuchProcess"})FALSEPS> test (@(get-Process |where {$_.name -eq "PowerShell"}).count -ne 0)TRUEPS> 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=10PS> test $xTRUEPS> $x=0PS> test $xFALSEPS> test $NoSuchVariableFALSEPS> set-psdebug -strictPS> $NoSuchVariableThe variable $NoSuchVariable cannot be retrieved because it has not been set 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 ArchitectVisit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShellVisit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
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.