where syntax

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.

This entry was posted in Powershell Basics, PowerShell V3, PowerShell v4. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s