Search code examples
powershellscriptblock

How to refer to entire pipeline object without a script block


With the advent of PowerShell V3 instead of having to write:

Get-Process | Where { $_.ProcessName -match "win" }

...one could now write the more terse:

Get-Process | Where ProcessName -match "win"

... a clear win (ahem) for shell use.

Now let's say I had a simple array of strings, call it $stuff. Is it possible to reduce this:

$stuff | Where { $_ -match "win" }

...in an analogous way to the first example, i.e. removing the script block and referring to the entire object, in this case?


Solution

  • To reduce

    $stuff | Where { $_ -match "win" } 
    

    you can always do it like this (works for all powershell version):

    $stuff = "a","b","win", "winona", "d", "windows"
    $stuff -match "win"
    win
    winona
    windows