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 演算子を使用して 3 つの条件付きステートメントを連結します。このステートメントは、$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) 

注記:

前の例では、比較演算子の等号 (-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