about_Logical_Operators
应用到: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0
主题
about_Logical_Operators
简短说明
介绍连接 Windows PowerShell® 中的语句的运算符。
详细说明
Windows PowerShell 逻辑运算符连接表达式和语句,从而允许你使用单个表达式来测试多个条件。
例如,下面的语句使用 and 运算符和 or 运算符连接三个条件语句。仅当 $a 的值大于 $b 的值,且 $a 或 $b 小于 20 时,该语句才为 true。
($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)
注意:
前面的示例也使用等号作为比较运算符 (-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