Tag Archives: powershell v3

Easier ForEach/Where-Object in Powershell V3

PowerTip of the Day, from PowerShell.com

In the upcoming PowerShell v3 which you can already download as a Beta version, using Where-Object and ForEach-Object becomes a lot simpler. No longer do you need a script block and code. This, for example, is all you need to find files larger than 1MB:

Get-ChildItem $env:windir | Where-Object Length -gt 1MB

Previously, you would have had to write:

Get-ChildItem $env:windir | Where-Object { $_.Length -gt 1MB }

Implicit Foreach in PSv3

PowerTip of the Day, from PowerShell.com:

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.

(Get-Process notepad).CloseMainWindow()

Previously, you would have had to write:

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