Logical operators
Logical operators evaluate two or more comparisons or other operations that produce a Boolean (true
or false
) result.
The following logic operators are available:
- And:
-and
- Or:
-or
- Exclusive or:
-xor
- Not:
-not
and!
And
The -and
operator returns $true
if the values on the left-hand and right-hand side are both $true
.
For example, each of the following returns $true
:
$true -and $true
1 -lt 2 -and "string" -like 's*'
1 -eq 1 -and 2 -eq 2 -and 3 -eq 3
(Test-Path C:\Windows) -and (Test-Path 'C:\Program Files')
Or
The -or
operator returns true
if the value on the left, or the value on the right, or both, are true
.
For example, each of the following returns $true
:
$true -or $true
2 -gt 1 -or "something" -ne "nothing"
1 -eq 1 -or 2 -eq 1
(Test-Path C:\Windows) -or (Test-Path D:\Windows)
Exclusive or
The -xor
operator will return $true
if...