{"id":9051,"date":"2006-12-24T17:03:00","date_gmt":"2006-12-24T17:03:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/12\/24\/boolean-values-and-operators\/"},"modified":"2019-02-18T13:21:01","modified_gmt":"2019-02-18T20:21:01","slug":"boolean-values-and-operators","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/boolean-values-and-operators\/","title":{"rendered":"Boolean Values and Operators"},"content":{"rendered":"<p><!--\n \/* Font Definitions *\/\n @font-face\n\t{font-family:\"Cambria Math\";\n\tpanose-1:2 4 5 3 5 4 6 3 2 4;}\n@font-face\n\t{font-family:Cambria;\n\tpanose-1:2 4 5 3 5 4 6 3 2 4;}\n@font-face\n\t{font-family:Calibri;\n\tpanose-1:2 15 5 2 2 2 4 3 2 4;}\n@font-face\n\t{font-family:\"Lucida Console\";\n\tpanose-1:2 11 6 9 4 5 4 2 2 4;}\n \/* Style Definitions *\/\n p.MsoNormal, li.MsoNormal, div.MsoNormal\n\t{margin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.75in;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\nh2\n\t{mso-style-link:\"Heading 2 Char\";\n\tmargin-top:10.0pt;\n\tmargin-right:0in;\n\tmargin-bottom:0in;\n\tmargin-left:.75in;\n\tmargin-bottom:.0001pt;\n\tline-height:115%;\n\tpage-break-after:avoid;\n\tfont-size:13.0pt;\n\tfont-family:\"Cambria\",\"serif\";\n\tcolor:#4F81BD;\n\tfont-weight:bold;}\na:link, span.MsoHyperlink\n\t{color:blue;\n\ttext-decoration:underline;}\na:visited, span.MsoHyperlinkFollowed\n\t{color:purple;\n\ttext-decoration:underline;}\np\n\t{margin-right:0in;\n\tmargin-left:0in;\n\tfont-size:12.0pt;\n\tfont-family:\"Times New Roman\",\"serif\";}\np.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph\n\t{margin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.5in;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\nspan.Heading2Char\n\t{mso-style-name:\"Heading 2 Char\";\n\tmso-style-link:\"Heading 2\";\n\tfont-family:\"Cambria\",\"serif\";\n\tcolor:#4F81BD;\n\tfont-weight:bold;}\np.msolistparagraphcxspfirst, li.msolistparagraphcxspfirst, div.msolistparagraphcxspfirst\n\t{mso-style-name:msolistparagraphcxspfirst;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:0in;\n\tmargin-left:.5in;\n\tmargin-bottom:.0001pt;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\np.msolistparagraphcxspmiddle, li.msolistparagraphcxspmiddle, div.msolistparagraphcxspmiddle\n\t{mso-style-name:msolistparagraphcxspmiddle;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:0in;\n\tmargin-left:.5in;\n\tmargin-bottom:.0001pt;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\np.msolistparagraphcxsplast, li.msolistparagraphcxsplast, div.msolistparagraphcxsplast\n\t{mso-style-name:msolistparagraphcxsplast;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.5in;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\nspan.CodeChar\n\t{mso-style-name:\"Code Char\";\n\tmso-style-link:Code;\n\tfont-family:\"Lucida Console\";\n\tcolor:#EEEDF0;\n\tbackground:#012456;}\np.Code, li.Code, div.Code\n\t{mso-style-name:Code;\n\tmso-style-link:\"Code Char\";\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:12.0pt;\n\tmargin-left:.75in;\n\tline-height:115%;\n\tbackground:#012456;\n\tfont-size:8.0pt;\n\tfont-family:\"Lucida Console\";\n\tcolor:#EEEDF0;}\np.msopapdefault, li.msopapdefault, div.msopapdefault\n\t{mso-style-name:msopapdefault;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.75in;\n\tline-height:115%;\n\tfont-size:12.0pt;\n\tfont-family:\"Times New Roman\",\"serif\";}\n.MsoChpDefault\n\t{font-size:10.0pt;}\n@page Section1\n\t{size:8.5in 11.0in;\n\tmargin:1.0in 1.0in 1.0in 1.0in;}\ndiv.Section1\n\t{page:Section1;}\n--><\/p>\n<div class=\"Section1\">\n<p class=\"MsoNormal\"><span>John Smith asked what TRUE and FALSE were in PowerShell on our newsgroup Microsoft.Public.Windows.PowerShell.&nbsp;&nbsp; The simple answer to this is $TRUE and $FALSE but the complete answer is a richer.&nbsp; PowerShell has a very rich notion of TRUE and FALSE.&nbsp; The best way to explain it is to show it.&nbsp; <\/span><\/p>\n<p class=\"Code\">PS&gt;&nbsp; function test ($VALUE) {<br \/>&gt;&gt; if ($VALUE) {<br \/>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host -ForegroundColor GREEN &#8220;TRUE&#8221;<br \/>&gt;&gt; } else {<br \/>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host -ForegroundColor RED&nbsp;&nbsp; &#8220;FALSE&#8221;<br \/>&gt;&gt; }<br \/>&gt;&gt; }<br \/>&gt;&gt;<br \/>PS&gt; test $TRUE<br \/><span>TRUE<\/span><br \/>PS&gt; test $FALSE<br \/><span>FALSE<\/span><br \/>PS&gt; test TRUE<br \/><span>TRUE<\/span><br \/>PS&gt; test FALSE<br \/><span>TRUE<\/span><\/p>\n<p class=\"MsoNormal\">The question a lot of PowerShell newbies ask is: &nbsp;\u2018Why is \u201cFALSE\u201d TRUE?\u201d&nbsp; &nbsp;In PowerShell, Strings can be evaluated as Booleans.&nbsp; If a string is ZERO length \u2013 it is false, otherwise it is TRUE.&nbsp; \u201cFALSE\u201d has 5 characters so it is TRUE.<\/p>\n<p class=\"Code\">PS&gt; test &#8220;SHORT STRING&#8221;<br \/><span>TRUE<\/span><br \/>PS&gt; test &#8220;&#8221;<br \/><span>FALSE<\/span><br \/>PS&gt; $x=(&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8221; -replace &#8221; &#8220;)<br \/>PS&gt; test $x<br \/><span>FALSE<\/span><br \/>PS&gt;<\/p>\n<p class=\"MsoNormal\">Given that see if you can figure out what is going on with the sequence below:<\/p>\n<p class=\"Code\">PS&gt; test &#8220;0&#8221;<br \/><span>TRUE<\/span><br \/>PS&gt; test 0<br \/><span>FALSE<\/span><br \/>PS&gt; test 1<br \/><span>TRUE<\/span><br \/>PS&gt; test 0.0<br \/><span>FALSE<\/span><br \/>PS&gt; test 0x0<br \/><span>FALSE<\/span><br \/>PS&gt; test 0mb<br \/><span>FALSE<\/span><br \/>PS&gt; test 0kb<br \/><span>FALSE<\/span><br \/>PS&gt; test 0D<br \/><span>FALSE<\/span><br \/>PS&gt; test 0.00000001<br \/><span>TRUE<\/span><br \/>PS&gt;<\/p>\n<p class=\"MsoNormal\">\u201c0\u201d is TRUE because it is a STRING and it has a length of 1.&nbsp; 0 is FALSE because it is a number and that number is 0.&nbsp; In PowerShell, any number which evaluates to 0 is FALSE and every non-zero number is TRUE.&nbsp; 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.&nbsp; <\/p>\n<p class=\"MsoNormal\">The next one should be a little easier to figure out.&nbsp; The last 2 lines shows you what you would have to type if we didn\u2019t have a rich concept of TRUE and FALSE. <\/p>\n<p class=\"Code\">PS&gt; $x=@(1,2,3,4,5)<br \/>PS&gt; test $x<br \/><span>TRUE<\/span><br \/>PS&gt; $x=@()<br \/>PS&gt; test $x<br \/><span>FALSE<\/span><br \/>PS&gt; test (get-Process)<br \/><span>TRUE<\/span><br \/>PS&gt; test (get-Process |where {$_.name -eq &#8220;PowerShell&#8221;})<br \/><span>TRUE<\/span><br \/>PS&gt; test (get-Process |where {$_.name -eq &#8220;NoSuchProcess&#8221;})<br \/><span>FALSE<\/span><br \/>PS&gt; test (@(get-Process |where {$_.name -eq &#8220;PowerShell&#8221;}).count -ne 0)<br \/><span>TRUE<\/span><br \/>PS&gt; test (@(get-Process |where {$_.name -eq &#8220;NoSuchProcess&#8221;}).count -ne 0)<br \/><span>FALSE<\/span><\/p>\n<p class=\"MsoNormal\">Here is your last example.&nbsp; If a variable is defined, we use its value to determine TRUE\/FALSE but if the variable is not defined \u2013 it is FALSE.<\/p>\n<p class=\"Code\">PS&gt; $x=10<br \/>PS&gt; test $x<br \/><span>TRUE<br \/><\/span>PS&gt; $x=0<br \/>PS&gt; test $x<br \/><span>FALSE<\/span><br \/>PS&gt; test $NoSuchVariable<br \/><span>FALSE<\/span><br \/>PS&gt; set-psdebug -strict<br \/>PS&gt; $NoSuchVariable<br \/><span>The variable $NoSuchVariable cannot be retrieved because it has not been se<br \/>t yet.<br \/>At line:1 char:15<br \/>+ $NoSuchVariable &lt;&lt;&lt;&lt;<\/span><br \/>PS&gt;<\/p>\n<p class=\"MsoNormal\">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.<\/p>\n<p class=\"MsoNormal\">Happy Holidays!<\/p>\n<p class=\"MsoNormal\">Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/MMC Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; http:\/\/blogs.msdn.com\/PowerShell<br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>John Smith asked what TRUE and FALSE were in PowerShell on our newsgroup Microsoft.Public.Windows.PowerShell.&nbsp;&nbsp; The simple answer to this is $TRUE and $FALSE but the complete answer is a richer.&nbsp; PowerShell has a very rich notion of TRUE and FALSE.&nbsp; The best way to explain it is to show it.&nbsp; PS&gt;&nbsp; function test ($VALUE) {&gt;&gt; [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>John Smith asked what TRUE and FALSE were in PowerShell on our newsgroup Microsoft.Public.Windows.PowerShell.&nbsp;&nbsp; The simple answer to this is $TRUE and $FALSE but the complete answer is a richer.&nbsp; PowerShell has a very rich notion of TRUE and FALSE.&nbsp; The best way to explain it is to show it.&nbsp; PS&gt;&nbsp; function test ($VALUE) {&gt;&gt; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=9051"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9051\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=9051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=9051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=9051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}