Is this just wrong? Double Where-Object to use the simplified syntax with –and.

When PowerShell v3 introduced the new simplified syntax for the where-object, at first I really loathed it

Get-Process | Where-Object {$ -gt 100MB}

can be simplified to this:

Get-Process | Where ws -gt 100MB

The reason I didn't like it up front was because you still really have to understand the first more complex example because the simplified syntax can only do very simple comparisons.  You cant do complex comparisons like joining two conditions with a –and operator.

It did finally grow on me though, and now, when no one is looking, I really love the newer simplified syntax.  So much so that when I have a –and situation, I actually have caught myself lately using two adjacent where-object instead of one using the simplified syntax.  I am sure there is ghastly performance impact of this, but for simple stuff, it works fine.  Now this wouldn't work for the –or operator, but it works great for –and.

Get-Process | Where-Object {$ -gt 100MB -and $ -gt 100MB}

can be written using two simplified where-object cmdlets

Get-Process | Where ws -gt 100MB | where pm -gt 100MB

Is this wrong?

I think from a pure coding standpoint its not a good behavior, but PowerShell’s target audience is admins.  PowerShell is a means to an end, it is not the end for admins.  I think simplicity and readability are huge and because of that I don't think this is too wrong.  You still cant get away without knowing the more complex –filterscript syntax, but this does seem more fun.

What do you think?


-Gary Siepser

Skip to main content