PowerShell v 3.0 Foreach-Object Loops

PowerShell v 3.0 Foreach-Object Changes

I want to draw your attention to changes to Foreach-Object in PowerShell 3.0, while highlighting the differences between the Foreach statement and the Foreach-Object cmdlet.

Topics for PowerShell 3.0’s Foreach-Object Loop

 ♣

Foreach-Object in PowerShell 2.0

This is the first half of my experiment:

# PowerShell 2.0 version of Foreach-Object
Clear-host
$Prog =Get-ChildItem ‘C:\program files\*.*’ -recurse
Measure-Command {$Prog | Foreach-Object {$_.name}
}

Note 1: My result was 170 ms.

Note 2: You could use the percentage sign as an alias % {$_.name}

Foreach-Object in PowerShell 3.0

The whole point is that in PowerShell 3.0 you don't need the {curly brackets}.  I don't approve of this shortcut, in my mind it's only going to lead to confusion.

# PowerShell 3.0 version of Foreach-Object
Clear-host
$Prog =Get-ChildItem ‘C:\program files\*.*’ -recurse
Measure-Command {$Prog | Foreach-Object $_.name
}

Note 3: My result was the same: 170 ms; removing the brackets made no difference.

Conclusion:  Not a big deal, PowerShell 3.0 allows you to remove the {} in the block.  We are comparing {$_.name} with $_.name.

Scripting Rationale

The whole rationale of scripting is speed up manual operations.  Thus, one of the key elements is looping through a set of instructions while applying a script block to each item.

PowerShell 3.0 loops have simpler syntax compared with versions 1 and 2. As a result beginners can create loops more easily, yet old-timers can reiterate using the same constructions they used in PowerShell 1.0.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Foreach Statement

Purely to compare and contrast with the examples above, here is the simpler statement version.  Its main benefit is speed.

Clear-Host
$Prog = Get-ChildItem ‘C:\program files\*.*’ -recurse
Measure-Command {Foreach ($item in $Prog) {$_.name}}

Note 4: My result was 16.561 ms

With the Foreach statement you cannot use the % alias.

If you get an error message test this snippet:
Get-ChildItem ‘C:\program files\*.*’ -recurse

 See more on PowerShell Measure-Command »

Recap of PowerShell’s Foreach Loop

Let us examine the PowerShell 2.0 loop syntax:
Foreach ($item in $collection-array) {command_block}.

Foreach is the PowerShell instruction to cycle or repeat; it feeds on an array of ($items in $collection-array) then you need a {Block statement payload}.

One more tiny, but crucial, component of the PowerShell foreach loop is ‘in’.

Simple PowerShell Foreach

# PowerShell Foreach Example
Clear-Host
Foreach ($number in 1..10 ) { $number * 25}

See examples of PowerShell v 3.0’s where »

Summary of PowerShell Foreach-Object Loops

In PowerShell 3.0 the Where-Object and Foreach-Object have made adding a set of {Curly} brackets optional.  The idea is to make the scripts easier to write for beginners.  However, you will notice many old-times still use the version 2.0 fuller syntax.

If you like this page then please share it with your friends

 


See more Windows PowerShell examples

PowerShell Home   • Foreach loops   • PowerShell Foreach   • Foreach-Object cmdlet

Syntax   • Variables   • -whatIf   • -ErrorAction   • Windows 8 PowerShell   • Free CSV Import Tool

PowerShell Functions   • [System.Math]   • Get-Credential   • Windows PowerShell   • PowerShell 3.0

Please email me if you have a better example script.  Also please report any factual mistakes, grammatical errors or broken links, I will be happy to  correct the fault.