Skip to content

Commit 63cd02a

Browse files
surfingoldelephantsdwheeler
authored andcommitted
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).
1 parent 9aa8eb2 commit 63cd02a

4 files changed

Lines changed: 91 additions & 3 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ parentheses to force the evaluation order, even when it forces the default
173173
operator precedence. The parentheses make your intentions clear to people who
174174
are reading and maintaining your scripts.
175175

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+
176197
## See also
177198

178199
- [about_Operators][ops]
@@ -197,3 +218,4 @@ are reading and maintaining your scripts.
197218
[scopes]: about_Scopes.md
198219
[split]: about_Split.md
199220
[type]: about_Type_Operators.md
221+
[wql]: about_WQL.md

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

Lines changed: 23 additions & 1 deletion
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-7.4&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Operator_Precedence
@@ -176,6 +176,27 @@ parentheses to force the evaluation order, even when it forces the default
176176
operator precedence. The parentheses make your intentions clear to people who
177177
are reading and maintaining your scripts.
178178

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

181202
- [about_Operators][ops]
@@ -200,3 +221,4 @@ are reading and maintaining your scripts.
200221
[scopes]: about_Scopes.md
201222
[split]: about_Split.md
202223
[type]: about_Type_Operators.md
224+
[wql]: about_WQL.md

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

Lines changed: 23 additions & 1 deletion
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-7.5&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Operator_Precedence
@@ -176,6 +176,27 @@ parentheses to force the evaluation order, even when it forces the default
176176
operator precedence. The parentheses make your intentions clear to people who
177177
are reading and maintaining your scripts.
178178

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

181202
- [about_Operators][ops]
@@ -200,3 +221,4 @@ are reading and maintaining your scripts.
200221
[scopes]: about_Scopes.md
201222
[split]: about_Split.md
202223
[type]: about_Type_Operators.md
224+
[wql]: about_WQL.md

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

Lines changed: 23 additions & 1 deletion
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-7.6&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Operator_Precedence
@@ -176,6 +176,27 @@ parentheses to force the evaluation order, even when it forces the default
176176
operator precedence. The parentheses make your intentions clear to people who
177177
are reading and maintaining your scripts.
178178

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

181202
- [about_Operators][ops]
@@ -200,3 +221,4 @@ are reading and maintaining your scripts.
200221
[scopes]: about_Scopes.md
201222
[split]: about_Split.md
202223
[type]: about_Type_Operators.md
224+
[wql]: about_WQL.md

0 commit comments

Comments
 (0)