This blog will discuss PowerShell boolean with appropriate examples.
How to use boolean in PowerShell?
In order to use PowerShell boolean, follow the below-given syntax.
Syntax
The given syntax will compare two values, “value1” and “value2,” based on the added “condition”. This expression will return PowerShell boolean “True” or “False” as an output:
Operator Precedence in PowerShell
We compare values with different conditions by using operators. During the comparison, PowerShell starts comparing values from left to right. If the left-sided value equals the right-sided value, it is considered true; otherwise, it is false.
Now, check out the following examples for using boolean in PowerShell.
Example 1
In this example, we will compare two values and check whether their word count is equal or not. To compare values, we will add the string “palindrome” with “-eq” or “equals to” operator:
The below code snippet shows “True” as an output because the word count of both of the added values is equal:
In case if the word count of the specified value is not equal, the expression will return “False” boolean value:
Output
Example 2
Now, in the same example, we will check the case sensitivity of the added values along with their word count. To do so, use “-ceq” instead of the “-eq” operator:
The given expression will return “True” as the values have the same case and word count:
Similarly, the expression given below will return “False” as word count and the case of the added values are not matched:
Example 3
In PowerShell, the boolean operator “$true” is equivalent to “1”:
Output
Similarly, the “$false” boolean value is equivalent to “0”:
Output
As “$true” is not equal to “0” so the following expression will output “False”:
Output
Lastly, “$false” is not equal to “1” so the following expression will output “False”:
Output
We have provided all the essential information related to PowerShell boolean with examples.
Conclusion
PowerShell uses boolean values to determine whether something is true or false. These values are denoted as $True or $False, where the “$True” boolean value is equivalent to “1” and “$False” is equivalent to “0”. PowerShell boolean types are quite helpful when working with scripts. In this blog, we discussed boolean in PowerShell with appropriate examples.