Quick Tip: New Implicit ForEach in PSv3

Quick Tip: New Implicit ForEach in PSv3

  • Comments 1
  • Likes

I saw this today in an email from PowerShell.com and had to pass it on… seems like PSv3 is going to have lots of little handy additions:

Implicit Foreach in PSv3

PowerShell v3 Beta is available for quite some time now, so every now and then we start tossing in some PowerShell v3 tips. We'll clearly mark them as such since they only run on PowerShell v3, not v2. This is something new in PowerShell v3:

When you work with array data, in previous PowerShell versions you had to loop through all elements to get to their properties and methods. No more in PowerShell v3. PowerShell now detects that you are working with an array and applies your call to all array elements. So with this line you could gracefully close all running notepad.exe.

PS> (Get-Process notepad).CloseMainWindow()

Previously, you would have had to write:

PS> Get-Process notepad| ForEach-Object { $_.CloseMainWindow() }

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • This is SUCH a dangerous idea.  This means that existing code in v2 will still run, but completely differently, in v3?!