PowerShell V.Next: Simplified Where-Object and ForEach-Object


I was perusing the samples that come with the PowerShell V3  CTP (Samples\WindowsPowerShell) when I noticed a folder called “Simplified Foreach and Where”

Lo and behold, you no longer need curly braces “{}” or the $_ to refer to the current object in the ForEach-Object and  Where-Object cmdlets!

The sample is incredibly simple, but quickly shows the power of this new feature:

001
002
003
004
005
006
007
008
009
010
# Suggestion: Simplified Where and Foreach
# This is the same demo that we showed during the Summit Preview live meeting
# We just like it so much that we want to show it again
# You no longer need {} and $_ to do Where-Object
Get-Process | where Name -eq powershell
# Same thing with Foreach-Object
notepad;notepad;notepad;
Get-Process | where name -eq notepad
Get-Process | where name -eq notepad | foreach kill
Get-Process | where name -eq notepad
Advertisements

  1. #1 by jvierra on December 20, 2011 - 20:54

    Not new…. Just amde more obvious.

  2. #2 by Peacefulheart on July 14, 2012 - 07:56

    Reblogged this on The PowerShell of Windows and commented:
    It is more convenient than the previous version using Where-object.

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

%d bloggers like this: