about_Logical_Operators
適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0
主題
about_Logical_Operators
簡短描述
描述在 Windows PowerShell® 中連接陳述式的運算子。
詳細描述
Windows PowerShell 邏輯運算子會連接運算式和陳述式,讓您使用單一運算式來測試多個條件。
例如,下列陳述式會使用 and 運算子和 or 運算子來連接三個條件式陳述式。只有 $a 值大於 $b 值 (不是 $a 就是 $b 小於 20) 時,此陳述式才成立。
($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))
Windows PowerShell 支援下列邏輯運算子。
Operator Description Example
-------- ------------------------------ ------------------------
-and Logical and. TRUE only when (1 -eq 1) -and (1 -eq 2)
both statements are TRUE. False
-or Logical or. TRUE when either (1 -eq 1) -or (1 -eq 2)
or both statements are TRUE. True
-xor Logical exclusive or. TRUE (1 -eq 1) -xor (2 -eq 2)
only when one of the statements False
is TRUE and the other is FALSE.
-not Logical not. Negates the -not (1 -eq 1)
statement that follows it. False
! Logical not. Negates the !(1 -eq 1)
statement that follows it. False
(Same as -not)
注意:
前面範例也可以使用 equal to 比較運算子 (-eq)。如需詳細資訊,請參閱 about_Comparison_Operators。範例也可使用整數布林值。整數 0 的值為 FALSE。所有其他整數的值均為 TRUE。
邏輯運算子的語法如下所示:
<statement> {-AND | -OR | -XOR} <statement>
{! | -NOT} <statement>
使用邏輯運算子的陳述式會傳回布林值 (TRUE 或 FALSE)。
Windows PowerShell 邏輯運算子只會評估用於判斷陳述式的真偽值所需的陳述式。如果陳述式中包含 and 運算子的左運算元為 FALSE,則不會評估右運算元。如果陳述式中包含 or 陳述式的左運算元為 TRUE,則不會評估右運算元。因此,您可以如同使用 If 陳述式一樣,使用這些陳述式。
另請參閱
about_Operators
Compare-Object
about_Comparison_operators
about_If