Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

ForEach or ForEach-Object

Posted on November 9, 2011

I came across a post the other day that explained differences when using the ForEach enumerator and the ForEach-Object cmdlet. They both essentially do the same thing but as the post mentions there are potential performance differences. One other difference I want to highlight is that the ForEach enumerator doesn't write to the pipeline at the end.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

By that, I mean a command like this will fail:

[cc lang="DOS"]
PS C:\> foreach ($i in (1..10)) {$i*2} | out-file $env:temp\data.txt
An empty pipe element is not allowed.
At line:1 char:33
+ foreach ($i in (1..10)) {$i*2} | <<<< $env:temp\out-file data.txt + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : EmptyPipeElement [/cc] Of course there are workarounds but you have to apply them to every pipelined object in the {}. Compare that to using ForEach-Object. [cc lang="DOS"] PS C:\> 1..10 | foreach-object {$_*2} | out-file $env:temp\data.txt
[/cc]

With the enumerator you get to name your own variable. With ForEach-Object you use the $_ placeholder. Personally, I think of the ForEach enumerator as a legacy construct from the VBScript days. That's not to say you should never use it, but you need to look at the bigger picture of what you are attempting to accomplish and make sure you are doing it the "PowerShell" way.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d