Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Describes the operators that connect statements in PowerShell.
Locale: en-US
ms.date: 08/29/2022
ms.date: 12/30/2025
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_logical_operators?view=powershell-5.1&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about_Logical_Operators
Expand All @@ -17,10 +17,35 @@ Describes the operators that connect statements in PowerShell.
The PowerShell logical operators connect expressions and statements, allowing
you to use a single expression to test for multiple conditions.

For example, the following statement uses the and operator and the or operator
to connect three conditional statements. The statement is true only when the
value of $a is greater than the value of $b, and either $a or $b is less than
20.
Statements that use the logical operators return boolean (TRUE or FALSE)
values.

The PowerShell logical operators evaluate only the statements required to
determine the truth value of the statement. If the left operand in a statement
that contains the `-and` operator is FALSE, the right operand isn't evaluated.
If the left operand in a statement that contains the `-or` statement is TRUE,
the right operand isn't evaluated. As a result, you can use these statements in
the same way that you would use the `if` statement.

> [!IMPORTANT]
> The `-and`, `-or` and `-xor` operators have equal precedence. They are
> evaluated from left to right as they appear within the expression. For more
> information, see [about_Operator_Precedence][01].

## Syntax

The syntax of the logical operators is as follows:

```Syntax
<statement> {-and | -or | -xor} <statement>
{! | -not} <statement>
```

## Examples

The following example uses the `-and` and `-or` operators to connect three
conditional statements. The result is TRUE only when the value of `$a` is
greater than the value of `$b`, and either `$a` or `$b` is less than `20`.

```powershell
($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))
Expand All @@ -46,38 +71,29 @@ PowerShell supports the following logical operators.
(1 -eq 1) -xor (2 -eq 2) # Result is False
```

- Logical not (`-not`) or (`!`) - Negates the statement that follows.
- Logical NOT (`-not`) or (`!`) - Negates the statement that follows.

```powershell
-not (1 -eq 1) # Result is False
!(1 -eq 1) # Result is False
```

The previous examples also use the equal to comparison operator `-eq`. For more
information, see [about_Comparison_Operators](about_Comparison_Operators.md).
The examples also use the Boolean values of integers. The integer 0 has a value
of FALSE. All other integers have a value of TRUE.

The syntax of the logical operators is as follows:

```Syntax
<statement> {-and | -or | -xor} <statement>
{! | -not} <statement>
```

Statements that use the logical operators return Boolean (TRUE or FALSE)
values.

The PowerShell logical operators evaluate only the statements required to
determine the truth value of the statement. If the left operand in a statement
that contains the and operator is FALSE, the right operand isn't evaluated. If
the left operand in a statement that contains the or statement is TRUE, the
right operand isn't evaluated. As a result, you can use these statements in
the same way that you would use the `if` statement.
The previous examples also use the equality comparison operator, `-eq`. For
more information, see [about_Comparison_Operators][03]. The examples also use
the boolean values of integers. The integer `0` has a boolean value of FALSE.
All other integers have a value of TRUE.

## See also

- [about_Operators](about_Operators.md)
- [about_Comparison_operators](about_Comparison_Operators.md)
- [about_If](about_If.md)
- [Compare-Object](xref:Microsoft.PowerShell.Utility.Compare-Object)
- [about_Operators][02]
- [about_Operator_Precedence][01]
- [about_Comparison_Operators][03]
- [about_If][04]
- [Compare-Object][05]

<!-- link references -->
[01]: about_Operator_Precedence.md
[02]: about_Operators.md
[03]: about_Comparison_Operators.md
[04]: about_If.md
[05]: xref:Microsoft.PowerShell.Utility.Compare-Object
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Lists the PowerShell operators in precedence order.
Locale: en-US
ms.date: 06/29/2021
ms.date: 12/30/2025
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_operator_precedence?view=powershell-5.1&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about_Operator_Precedence
Expand Down Expand Up @@ -33,15 +33,15 @@ evaluated. Operators on the same line, or in the same group, have equal
precedence.

The Operator column lists the operators. The Reference column lists the
PowerShell Help topic in which the operator is described. To display the topic,
type `Get-Help <topic-name>`.
PowerShell Help topic in which the operator is described. To display the topic
interactively, use `Get-Help -Name <topic-name>`.

| OPERATOR | REFERENCE |
| --------------------------- | ------------------------------------ |
| `$() @() () @{}` | [about_Operators][ops] |
| `. ?.` (member access) | [about_Operators][ops] |
| `.` (member access) | [about_Operators][ops] |
| `::` (static) | [about_Operators][ops] |
| `[0] ?[0]` (index operator) | [about_Operators][ops] |
| `[0]` (index operator) | [about_Operators][ops] |
| `[int]` (cast operators) | [about_Operators][ops] |
| `-split` (unary) | [about_Split][split] |
| `-join` (unary) | [about_Join][join] |
Expand Down Expand Up @@ -85,16 +85,13 @@ that happens.
| ------------------------------------------------------- | ------------------------------------ |
| `.` (dot-source) | [about_Operators][ops] |
| `&` (call) | [about_Operators][ops] |
| `? <if-true> : <if-false>` (Ternary operator) | [about_Operators][ops] |
| `??` (null-coalese operator) | [about_Operators][ops] |
| <code>&#124;</code> (pipeline operator) | [about_Operators][ops] |
| `> >> 2> 2>> 2>&1` | [about_Redirection][redir] |
| <code>&& &#124;&#124;</code> (pipeline chain operators) | [about_Operators][ops] |
| `= += -= *= /= %= ??=` | [about_Assignment_Operators][assign] |
| `= += -= *= /= %=` | [about_Assignment_Operators][assign] |

## Examples

The following two commands show the arithmetic operators and the effect of
The following two examples show the arithmetic operators and the effect of
using parentheses to force PowerShell to evaluate the enclosed part of the
expression first.

Expand All @@ -107,28 +104,28 @@ PS> (2 + 3) * 4
```

The following example gets the read-only text files from the local directory
and saves them in the `$read_only` variable.
and saves them in the `$readOnly` variable.

```powershell
$read_only = Get-ChildItem *.txt | Where-Object {$_.IsReadOnly}
$readOnly = Get-ChildItem -Path *.txt | Where-Object { $_.IsReadOnly }
```

It is equivalent to the following example.

```powershell
$read_only = ( Get-ChildItem *.txt | Where-Object {$_.IsReadOnly} )
$readOnly = (Get-ChildItem -Path *.txt | Where-Object { $_.IsReadOnly })
```

Because the pipeline operator (`|`) has a higher precedence than the assignment
operator (`=`), the files that the `Get-ChildItem` cmdlet gets are sent to the
`Where-Object` cmdlet for filtering before they are assigned to the
`$read_only` variable.
`$readOnly` variable.

The following example demonstrates that the index operator takes precedence
over the cast operator.

This expression creates an array of three strings. Then, it uses the index
operator with a value of 0 to select the first object in the array, which is
operator with a value of `0` to select the first object in the array, which is
the first string. Finally, it casts the selected object as a string. In this
case, the cast has no effect.

Expand Down Expand Up @@ -163,7 +160,7 @@ PS> (2 -gt 4) -and 1
False
```

If the -and operator had higher precedence, the answer would be TRUE.
If the `-and` operator had higher precedence, the result would be TRUE.

```powershell
PS> 2 -gt (4 -and 1)
Expand All @@ -176,6 +173,27 @@ parentheses to force the evaluation order, even when it forces the default
operator precedence. The parentheses make your intentions clear to people who
are reading and maintaining your scripts.

The following example demonstrates the precedence between the `-and` and `-or`
logical operators.

```powershell
PS> $true -or $false -and $false
False
```

In other languages such as C#, logical AND typically has a higher precedence
than logical OR, so you may expect the above expression to yield TRUE.

However, the `-and` and `-or` operators have equal precedence in PowerShell.
They are evaluated from the left to right as they appear within the expression.
As `$true -or $false` is TRUE and `$true -and $false` is FALSE, the result of
the expression is FALSE.

> [!IMPORTANT]
> Other contexts within PowerShell such as [WMI Query Language (WQL)][wql] and
> the Active Directory filter have their own operator precedence that might
> differ from PowerShell logical operator precedence.

## See also

- [about_Operators][ops]
Expand All @@ -189,7 +207,7 @@ are reading and maintaining your scripts.
- [about_Split][split]
- [about_Type_Operators][type]

<!-- reference links -->
<!-- link references -->
[math]: about_Arithmetic_Operators.md
[assign]: about_Assignment_Operators.md
[compare]: about_Comparison_Operators.md
Expand All @@ -200,3 +218,4 @@ are reading and maintaining your scripts.
[scopes]: about_Scopes.md
[split]: about_Split.md
[type]: about_Type_Operators.md
[wql]: about_WQL.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Describes the operators that connect statements in PowerShell.
Locale: en-US
ms.date: 08/29/2022
ms.date: 12/30/2025
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_logical_operators?view=powershell-7.4&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about_Logical_Operators
Expand All @@ -17,10 +17,35 @@ Describes the operators that connect statements in PowerShell.
The PowerShell logical operators connect expressions and statements, allowing
you to use a single expression to test for multiple conditions.

For example, the following statement uses the and operator and the or operator
to connect three conditional statements. The statement is true only when the
value of $a is greater than the value of $b, and either $a or $b is less than
20.
Statements that use the logical operators return boolean (TRUE or FALSE)
values.

The PowerShell logical operators evaluate only the statements required to
determine the truth value of the statement. If the left operand in a statement
that contains the `-and` operator is FALSE, the right operand isn't evaluated.
If the left operand in a statement that contains the `-or` statement is TRUE,
the right operand isn't evaluated. As a result, you can use these statements in
the same way that you would use the `if` statement.

> [!IMPORTANT]
> The `-and`, `-or` and `-xor` operators have equal precedence. They are
> evaluated from left to right as they appear within the expression. For more
> information, see [about_Operator_Precedence][01].

## Syntax

The syntax of the logical operators is as follows:

```Syntax
<statement> {-and | -or | -xor} <statement>
{! | -not} <statement>
```

## Examples

The following example uses the `-and` and `-or` operators to connect three
conditional statements. The result is TRUE only when the value of `$a` is
greater than the value of `$b`, and either `$a` or `$b` is less than `20`.

```powershell
($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))
Expand All @@ -46,38 +71,29 @@ PowerShell supports the following logical operators.
(1 -eq 1) -xor (2 -eq 2) # Result is False
```

- Logical not (`-not`) or (`!`) - Negates the statement that follows.
- Logical NOT (`-not`) or (`!`) - Negates the statement that follows.

```powershell
-not (1 -eq 1) # Result is False
!(1 -eq 1) # Result is False
```

The previous examples also use the equal to comparison operator `-eq`. For more
information, see [about_Comparison_Operators](about_Comparison_Operators.md).
The examples also use the Boolean values of integers. The integer 0 has a value
of FALSE. All other integers have a value of TRUE.

The syntax of the logical operators is as follows:

```Syntax
<statement> {-and | -or | -xor} <statement>
{! | -not} <statement>
```

Statements that use the logical operators return Boolean (TRUE or FALSE)
values.

The PowerShell logical operators evaluate only the statements required to
determine the truth value of the statement. If the left operand in a statement
that contains the and operator is FALSE, the right operand isn't evaluated. If
the left operand in a statement that contains the or statement is TRUE, the
right operand isn't evaluated. As a result, you can use these statements in
the same way that you would use the `if` statement.
The previous examples also use the equality comparison operator, `-eq`. For
more information, see [about_Comparison_Operators][03]. The examples also use
the boolean values of integers. The integer `0` has a boolean value of FALSE.
All other integers have a value of TRUE.

## See also

- [about_Operators](about_Operators.md)
- [about_Comparison_operators](about_Comparison_Operators.md)
- [about_If](about_If.md)
- [Compare-Object](xref:Microsoft.PowerShell.Utility.Compare-Object)
- [about_Operators][02]
- [about_Operator_Precedence][01]
- [about_Comparison_Operators][03]
- [about_If][04]
- [Compare-Object][05]

<!-- link references -->
[01]: about_Operator_Precedence.md
[02]: about_Operators.md
[03]: about_Comparison_Operators.md
[04]: about_If.md
[05]: xref:Microsoft.PowerShell.Utility.Compare-Object
Loading