PowerShell Logical Operators

Last time we saw the PowerShell Comparison Operators. This post will be about PowerShell Logical Operators. Logical Operators allow us to connect different statements together and/or expression. By doing so, we will be able to have one more complex expression based on different conditions and statements. The operators are very important and will allows us to create filters of the data that we retrieve from systems and also we will be able to create our custom checks of conditions. The below example uses all the logical operators that are allowed in PowerShell and some of the comparison operators that we saw in last tutorial. The example below will return “Hello World” to the console in green color.

Example:

$a = 1 $b = 2 $c = 3 $msg = "Hello World" $d = -not ((($a -eq 1) -and ($b -gt $a)) -or (($b -lt $c) -xor ($a -ge $b))) if ($d -eq $false){ Write-Host $msg -ForegroundColor Green}

Answer screenshot:

PowerShell Logical Operators List:

-and

-or

-xor

-not

Now lets go through the list and explain each operators along with some examples that we can use them.

And

The -and operator will return true when both statements are correct. If there are more statement and only -and operator is used, then all statements should be correct.

Examples

PS S:\> $a = 1 PS S:\> $b = 2 PS S:\> ($a -eq 1) -and ($b -eq 2) True PS S:\> ($a -eq 1) -and ($b -eq 3) False PS S:\> ($a -eq 3) -and ($b -eq 2) False PS S:\> ($a -eq 3) -and ($b -eq 4) False PS S:\>

Or

The -or operator will return true if at least one of the statements are correct. It does not matter how many statements you are using. If only one them is true then all the expression is true.

Examples

PS S:\> $a = 1 PS S:\> $b = 2 PS S:\> ($a -eq 1) -or ($b -eq 2) True PS S:\> ($a -eq 1) -or ($b -eq 3) True PS S:\> ($a -eq 3) -or ($b -eq 2) True PS S:\> ($a -eq 3) -or ($b -eq 4) False PS S:\>

Xor

The -xor operator returns true, if ONLY one statement in an expression is true. It is called exclusive or. It does not matter how many statements you are using in an expression. If more than one statement is true then the expression will return false.

Examples

PS S:\> $a = 1 PS S:\> $b = 2 PS S:\> ($a -eq 1) -xor ($b -eq 2) False PS S:\> ($a -eq 1) -xor ($b -eq 3) True PS S:\> ($a -eq 3) -xor ($b -eq 2) True PS S:\> ($a -eq 3) -xor ($b -eq 4) False PS S:\>

Not

The -not operator will turn the result of a statement to the opposite. If the return value is true for a statement then it will return false and the opposite.

Examples

PS S:\> $a = 1 PS S:\> $b = 2 PS S:\> $a -eq 1 True PS S:\> -not ($a -eq 1) False PS S:\> -not (($a -eq 1) -xor ($b -eq 2)) True PS S:\> -not (($a -eq 1) -xor ($b -eq 3)) False PS S:\> -not (($a -eq 3) -xor ($b -eq 2)) False PS S:\> -not (($a -eq 3) -xor ($b -eq 4)) True PS S:\>

Summary

Logical operators help us to combine our statements and create complex expressions to test our results in a script or filter them according to our needs. Logical Operators in combination with the comparison operators two subjects that you have to understand in order to be able to write effective scripts and filters.

I hope the tutorial about logical operators is helpful.

Please let me know your comments and thoughts. You feedback is appreciated.

Related Links: