|
2 | 2 | external help file: System.Management.Automation.dll-Help.xml |
3 | 3 | Locale: en-US |
4 | 4 | Module Name: Microsoft.PowerShell.Core |
5 | | -ms.date: 04/26/2024 |
| 5 | +ms.date: 04/23/2025 |
6 | 6 | online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/where-object?view=powershell-5.1&WT.mc_id=ps-gethelp |
7 | 7 | schema: 2.0.0 |
8 | 8 | title: Where-Object |
@@ -241,32 +241,38 @@ particular version of Windows. |
241 | 241 | Starting in Windows PowerShell 3.0, there are two different ways to construct a `Where-Object` |
242 | 242 | command. |
243 | 243 |
|
244 | | -- **Script block**. You can use a script block to specify the property name, a comparison operator, |
| 244 | +- **Script block syntax**. You can use a script block to specify the property name, a comparison operator, |
245 | 245 | and a property value. `Where-Object` returns all objects for which the script block statement is |
246 | 246 | true. |
247 | 247 |
|
248 | | - For example, the following command gets processes in the `Normal` priority class, that is, |
249 | | - processes where the value of the **PriorityClass** property equals `Normal`. |
| 248 | + For example, the following command gets processes where the value of the **PriorityClass** |
| 249 | + property equals `Normal`. |
250 | 250 |
|
251 | 251 | `Get-Process | Where-Object {$_.PriorityClass -eq "Normal"}` |
252 | 252 |
|
253 | 253 | All PowerShell comparison operators are valid in the script block format. For more information, |
254 | 254 | see [about_Comparison_Operators](./About/about_Comparison_Operators.md). |
255 | 255 |
|
256 | | -- **Comparison statement**. You can also write a comparison statement, which is much more like |
257 | | - natural language. Comparison statements were introduced in Windows PowerShell 3.0. |
| 256 | +- **Simplified syntax**. To enable the simiplified syntax, `Where-Object` includes 31 switch |
| 257 | + parameters that represent the comparison operators. The simplified syntax is easier to read and |
| 258 | + write than the script block syntax. You can combine one of the switch parameters with the |
| 259 | + **Property** and **Value** parameters to create a command that filters objects based on the |
| 260 | + values of their properties. |
258 | 261 |
|
259 | 262 | For example, the following commands also get processes that have a priority class of `Normal`. |
260 | 263 | These commands are equivalent and you can use them interchangeably. |
261 | 264 |
|
262 | | - `Get-Process | Where-Object -Property PriorityClass -EQ -Value "Normal"` |
| 265 | + `Get-Process | Where-Object -Property PriorityClass -Value Normal -EQ` |
263 | 266 |
|
264 | | - `Get-Process | Where-Object PriorityClass -EQ "Normal"` |
| 267 | + `Get-Process | Where-Object PriorityClass -EQ Normal` |
265 | 268 |
|
266 | | - Starting in Windows PowerShell 3.0, `Where-Object` adds comparison operators as parameters in a |
267 | | - `Where-Object` command. Unless specified, all operators are case-insensitive. Before Windows |
268 | | - PowerShell 3.0, the comparison operators in the PowerShell language were only usable in script |
269 | | - blocks. |
| 269 | + As shown in the example, the parameter names **Property** and **Value** are optional. The |
| 270 | + **Property** parameter is a positional parameter mapped to position `0`. The **Value** parameter |
| 271 | + is a positional parameter mapped to position `1`. The switch parameter, used to specify the |
| 272 | + comparison, can be used in any position. |
| 273 | + |
| 274 | + The simplfied syntax was introduced in Windows PowerShell 3.0. For more information, see |
| 275 | + [about_Simplified_Syntax](About/about_Simplified_Syntax.md). |
270 | 276 |
|
271 | 277 | When you provide a single **Property** to `Where-Object`, the cmdlet treats the value of the |
272 | 278 | property as a boolean expression. When the value of the property's **Length** isn't zero, the |
@@ -1181,4 +1187,4 @@ You can read more about these methods here [about_Arrays](./About/about_Arrays.m |
1181 | 1187 |
|
1182 | 1188 | [Tee-Object](../Microsoft.PowerShell.Utility/Tee-Object.md) |
1183 | 1189 |
|
1184 | | -[about_Booleans](about/about_Booleans.md) |
| 1190 | +[about_Booleans](./About/about_Booleans.md) |
0 commit comments