Powershell

Powershell boolean | Explained

We often utilize logic in PowerShell scripts to perform an action based on the condition that something is true. However, there come situations where you may want to handle the inverse of the added condition. In such a case, use boolean values in PowerShell 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”.

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:

<value1> condition <value2>

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:

> "palindrome" -eq "palindrome"

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:

> "palindrome" -eq "palindrom"

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:

> "palindrome" -ceq "palindrome"

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:

> "palindrome" -ceq "PALIND"

Example 3
In PowerShell, the boolean operator “$true” is equivalent to “1”:

> $true -eq 1

Output

Similarly, the “$false” boolean value is equivalent to “0”:

> $false -eq 0

Output

As “$true” is not equal to “0” so the following expression will output “False”:

> $true -eq 0

Output

Lastly, “$false” is not equal to “1” so the following expression will output “False”:

> $false -eq 1

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.