about_Logical_Operators

업데이트 날짜: 2014년 5월

적용 대상: 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보다 작은 경우에만 ture입니다.

        ($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