Skip to content

Commit 3bef92d

Browse files
Restructure about_Logical_Operators and add -and/-or precedence example (#12622)
* Restructure about_Logical_Operators This shifts about some of the existing content and adds a new header for Syntax. It also adds link references and makes minor formatting changes. * Add -and/-or/-xor equal precedence note * Remove PS 6+ operators from PS 5.1 doc This removes operators added in newer PS versions from the PS 5.1 about_Operator_Precedence doc. The PS 5.1 about_Operators doc already doesn't include new operators like ??. * Add -and/-or precedence example This adds an example involving both -and and -or to about_Operator_Precedence. It demonstrates the equal precedence between the two operators and calls out how this differs from other languages such as C#. It also notes that other contexts *within* PowerShell such as WQL and the AD filter have their own operator precedence which might differ (in both aforementioned cases, AND has higher precedence than OR). * Minor formatting/verbiage changes * Copy about_WQL to 7.x versions * Copy about_WMI to 7.x * fix link --------- Co-authored-by: Sean Wheeler <sean.wheeler@microsoft.com>
1 parent b3ed0a9 commit 3bef92d

File tree

14 files changed

+2816
-180
lines changed

14 files changed

+2816
-180
lines changed
Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes the operators that connect statements in PowerShell.
33
Locale: en-US
4-
ms.date: 08/29/2022
4+
ms.date: 12/30/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_logical_operators?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Logical_Operators
@@ -17,10 +17,35 @@ Describes the operators that connect statements in PowerShell.
1717
The PowerShell logical operators connect expressions and statements, allowing
1818
you to use a single expression to test for multiple conditions.
1919

20-
For example, the following statement uses the and operator and the or operator
21-
to connect three conditional statements. The statement is true only when the
22-
value of $a is greater than the value of $b, and either $a or $b is less than
23-
20.
20+
Statements that use the logical operators return boolean (TRUE or FALSE)
21+
values.
22+
23+
The PowerShell logical operators evaluate only the statements required to
24+
determine the truth value of the statement. If the left operand in a statement
25+
that contains the `-and` operator is FALSE, the right operand isn't evaluated.
26+
If the left operand in a statement that contains the `-or` statement is TRUE,
27+
the right operand isn't evaluated. As a result, you can use these statements in
28+
the same way that you would use the `if` statement.
29+
30+
> [!IMPORTANT]
31+
> The `-and`, `-or` and `-xor` operators have equal precedence. They are
32+
> evaluated from left to right as they appear within the expression. For more
33+
> information, see [about_Operator_Precedence][01].
34+
35+
## Syntax
36+
37+
The syntax of the logical operators is as follows:
38+
39+
```Syntax
40+
<statement> {-and | -or | -xor} <statement>
41+
{! | -not} <statement>
42+
```
43+
44+
## Examples
45+
46+
The following example uses the `-and` and `-or` operators to connect three
47+
conditional statements. The result is TRUE only when the value of `$a` is
48+
greater than the value of `$b`, and either `$a` or `$b` is less than `20`.
2449

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

49-
- Logical not (`-not`) or (`!`) - Negates the statement that follows.
74+
- Logical NOT (`-not`) or (`!`) - Negates the statement that follows.
5075

5176
```powershell
5277
-not (1 -eq 1) # Result is False
5378
!(1 -eq 1) # Result is False
5479
```
5580

56-
The previous examples also use the equal to comparison operator `-eq`. For more
57-
information, see [about_Comparison_Operators](about_Comparison_Operators.md).
58-
The examples also use the Boolean values of integers. The integer 0 has a value
59-
of FALSE. All other integers have a value of TRUE.
60-
61-
The syntax of the logical operators is as follows:
62-
63-
```Syntax
64-
<statement> {-and | -or | -xor} <statement>
65-
{! | -not} <statement>
66-
```
67-
68-
Statements that use the logical operators return Boolean (TRUE or FALSE)
69-
values.
70-
71-
The PowerShell logical operators evaluate only the statements required to
72-
determine the truth value of the statement. If the left operand in a statement
73-
that contains the and operator is FALSE, the right operand isn't evaluated. If
74-
the left operand in a statement that contains the or statement is TRUE, the
75-
right operand isn't evaluated. As a result, you can use these statements in
76-
the same way that you would use the `if` statement.
81+
The previous examples also use the equality comparison operator, `-eq`. For
82+
more information, see [about_Comparison_Operators][03]. The examples also use
83+
the boolean values of integers. The integer `0` has a boolean value of FALSE.
84+
All other integers have a value of TRUE.
7785

7886
## See also
7987

80-
- [about_Operators](about_Operators.md)
81-
- [about_Comparison_operators](about_Comparison_Operators.md)
82-
- [about_If](about_If.md)
83-
- [Compare-Object](xref:Microsoft.PowerShell.Utility.Compare-Object)
88+
- [about_Operators][02]
89+
- [about_Operator_Precedence][01]
90+
- [about_Comparison_Operators][03]
91+
- [about_If][04]
92+
- [Compare-Object][05]
93+
94+
<!-- link references -->
95+
[01]: about_Operator_Precedence.md
96+
[02]: about_Operators.md
97+
[03]: about_Comparison_Operators.md
98+
[04]: about_If.md
99+
[05]: xref:Microsoft.PowerShell.Utility.Compare-Object

reference/5.1/Microsoft.PowerShell.Core/About/about_Operator_Precedence.md

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Lists the PowerShell operators in precedence order.
33
Locale: en-US
4-
ms.date: 06/29/2021
4+
ms.date: 12/30/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_operator_precedence?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Operator_Precedence
@@ -33,15 +33,15 @@ evaluated. Operators on the same line, or in the same group, have equal
3333
precedence.
3434

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

3939
| OPERATOR | REFERENCE |
4040
| --------------------------- | ------------------------------------ |
4141
| `$() @() () @{}` | [about_Operators][ops] |
42-
| `. ?.` (member access) | [about_Operators][ops] |
42+
| `.` (member access) | [about_Operators][ops] |
4343
| `::` (static) | [about_Operators][ops] |
44-
| `[0] ?[0]` (index operator) | [about_Operators][ops] |
44+
| `[0]` (index operator) | [about_Operators][ops] |
4545
| `[int]` (cast operators) | [about_Operators][ops] |
4646
| `-split` (unary) | [about_Split][split] |
4747
| `-join` (unary) | [about_Join][join] |
@@ -85,16 +85,13 @@ that happens.
8585
| ------------------------------------------------------- | ------------------------------------ |
8686
| `.` (dot-source) | [about_Operators][ops] |
8787
| `&` (call) | [about_Operators][ops] |
88-
| `? <if-true> : <if-false>` (Ternary operator) | [about_Operators][ops] |
89-
| `??` (null-coalese operator) | [about_Operators][ops] |
9088
| <code>&#124;</code> (pipeline operator) | [about_Operators][ops] |
9189
| `> >> 2> 2>> 2>&1` | [about_Redirection][redir] |
92-
| <code>&& &#124;&#124;</code> (pipeline chain operators) | [about_Operators][ops] |
93-
| `= += -= *= /= %= ??=` | [about_Assignment_Operators][assign] |
90+
| `= += -= *= /= %=` | [about_Assignment_Operators][assign] |
9491

9592
## Examples
9693

97-
The following two commands show the arithmetic operators and the effect of
94+
The following two examples show the arithmetic operators and the effect of
9895
using parentheses to force PowerShell to evaluate the enclosed part of the
9996
expression first.
10097

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

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

112109
```powershell
113-
$read_only = Get-ChildItem *.txt | Where-Object {$_.IsReadOnly}
110+
$readOnly = Get-ChildItem -Path *.txt | Where-Object { $_.IsReadOnly }
114111
```
115112

116113
It is equivalent to the following example.
117114

118115
```powershell
119-
$read_only = ( Get-ChildItem *.txt | Where-Object {$_.IsReadOnly} )
116+
$readOnly = (Get-ChildItem -Path *.txt | Where-Object { $_.IsReadOnly })
120117
```
121118

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

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

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

@@ -163,7 +160,7 @@ PS> (2 -gt 4) -and 1
163160
False
164161
```
165162

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

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

176+
The following example demonstrates the precedence between the `-and` and `-or`
177+
logical operators.
178+
179+
```powershell
180+
PS> $true -or $false -and $false
181+
False
182+
```
183+
184+
In other languages such as C#, logical AND typically has a higher precedence
185+
than logical OR, so you may expect the above expression to yield TRUE.
186+
187+
However, the `-and` and `-or` operators have equal precedence in PowerShell.
188+
They are evaluated from the left to right as they appear within the expression.
189+
As `$true -or $false` is TRUE and `$true -and $false` is FALSE, the result of
190+
the expression is FALSE.
191+
192+
> [!IMPORTANT]
193+
> Other contexts within PowerShell such as [WMI Query Language (WQL)][wql] and
194+
> the Active Directory filter have their own operator precedence that might
195+
> differ from PowerShell logical operator precedence.
196+
179197
## See also
180198

181199
- [about_Operators][ops]
@@ -189,7 +207,7 @@ are reading and maintaining your scripts.
189207
- [about_Split][split]
190208
- [about_Type_Operators][type]
191209

192-
<!-- reference links -->
210+
<!-- link references -->
193211
[math]: about_Arithmetic_Operators.md
194212
[assign]: about_Assignment_Operators.md
195213
[compare]: about_Comparison_Operators.md
@@ -200,3 +218,4 @@ are reading and maintaining your scripts.
200218
[scopes]: about_Scopes.md
201219
[split]: about_Split.md
202220
[type]: about_Type_Operators.md
221+
[wql]: about_WQL.md
Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes the operators that connect statements in PowerShell.
33
Locale: en-US
4-
ms.date: 08/29/2022
4+
ms.date: 12/30/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_logical_operators?view=powershell-7.4&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Logical_Operators
@@ -17,10 +17,35 @@ Describes the operators that connect statements in PowerShell.
1717
The PowerShell logical operators connect expressions and statements, allowing
1818
you to use a single expression to test for multiple conditions.
1919

20-
For example, the following statement uses the and operator and the or operator
21-
to connect three conditional statements. The statement is true only when the
22-
value of $a is greater than the value of $b, and either $a or $b is less than
23-
20.
20+
Statements that use the logical operators return boolean (TRUE or FALSE)
21+
values.
22+
23+
The PowerShell logical operators evaluate only the statements required to
24+
determine the truth value of the statement. If the left operand in a statement
25+
that contains the `-and` operator is FALSE, the right operand isn't evaluated.
26+
If the left operand in a statement that contains the `-or` statement is TRUE,
27+
the right operand isn't evaluated. As a result, you can use these statements in
28+
the same way that you would use the `if` statement.
29+
30+
> [!IMPORTANT]
31+
> The `-and`, `-or` and `-xor` operators have equal precedence. They are
32+
> evaluated from left to right as they appear within the expression. For more
33+
> information, see [about_Operator_Precedence][01].
34+
35+
## Syntax
36+
37+
The syntax of the logical operators is as follows:
38+
39+
```Syntax
40+
<statement> {-and | -or | -xor} <statement>
41+
{! | -not} <statement>
42+
```
43+
44+
## Examples
45+
46+
The following example uses the `-and` and `-or` operators to connect three
47+
conditional statements. The result is TRUE only when the value of `$a` is
48+
greater than the value of `$b`, and either `$a` or `$b` is less than `20`.
2449

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

49-
- Logical not (`-not`) or (`!`) - Negates the statement that follows.
74+
- Logical NOT (`-not`) or (`!`) - Negates the statement that follows.
5075

5176
```powershell
5277
-not (1 -eq 1) # Result is False
5378
!(1 -eq 1) # Result is False
5479
```
5580

56-
The previous examples also use the equal to comparison operator `-eq`. For more
57-
information, see [about_Comparison_Operators](about_Comparison_Operators.md).
58-
The examples also use the Boolean values of integers. The integer 0 has a value
59-
of FALSE. All other integers have a value of TRUE.
60-
61-
The syntax of the logical operators is as follows:
62-
63-
```Syntax
64-
<statement> {-and | -or | -xor} <statement>
65-
{! | -not} <statement>
66-
```
67-
68-
Statements that use the logical operators return Boolean (TRUE or FALSE)
69-
values.
70-
71-
The PowerShell logical operators evaluate only the statements required to
72-
determine the truth value of the statement. If the left operand in a statement
73-
that contains the and operator is FALSE, the right operand isn't evaluated. If
74-
the left operand in a statement that contains the or statement is TRUE, the
75-
right operand isn't evaluated. As a result, you can use these statements in
76-
the same way that you would use the `if` statement.
81+
The previous examples also use the equality comparison operator, `-eq`. For
82+
more information, see [about_Comparison_Operators][03]. The examples also use
83+
the boolean values of integers. The integer `0` has a boolean value of FALSE.
84+
All other integers have a value of TRUE.
7785

7886
## See also
7987

80-
- [about_Operators](about_Operators.md)
81-
- [about_Comparison_operators](about_Comparison_Operators.md)
82-
- [about_If](about_If.md)
83-
- [Compare-Object](xref:Microsoft.PowerShell.Utility.Compare-Object)
88+
- [about_Operators][02]
89+
- [about_Operator_Precedence][01]
90+
- [about_Comparison_Operators][03]
91+
- [about_If][04]
92+
- [Compare-Object][05]
93+
94+
<!-- link references -->
95+
[01]: about_Operator_Precedence.md
96+
[02]: about_Operators.md
97+
[03]: about_Comparison_Operators.md
98+
[04]: about_If.md
99+
[05]: xref:Microsoft.PowerShell.Utility.Compare-Object

0 commit comments

Comments
 (0)