header image

where syntax

Posted by: | October 29, 2013 | No Comment |

This type of construction has been part of PowerShell since the very beginning:

Get-Process | where {$_.Handles -gt 500}


Get some data and use where-object to filter the data based on the value of some property. $_ represents the object on the pipeline.

PowerShell 3.0 introduced a simpler syntax

Get-Process | where Handles -gt 500

which is shorthand for

Get-Process | where -Property Handles -gt -Value 500


At which point it becomes obvious what is happening – you are comparing a property against a value. There are a whole set of comparison operators you can use in this manner – see the help file for details.

This new syntax makes life much easier when you only have a single comparison to perform – you need to use the old style syntax if you need to test on two properties.

The problem is that I don’t see this syntax being used that much. If you are using PowerShell 3.0 or above I recommend changing to the new style syntax.  It does save typing.

under: PowerShell Basics, PowerShell V3, PowerShell v4