Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

What’s the Differences Between ForEach vs ForEach-Object in PowerShell

November 9, 2022
in Blog, Powershell
0
ADVERTISEMENT
$names | ForEach -Process { Write-Output $_}
$names | ForEach { Write-Output $_}
$names | % { Write-Output $_}

PowerShell’s three lookalike friends can confuse, especially for newcomers. Basically, you’ve got two entities:

  • The ForEach-Object cmdlet, which has an alias ForEach (it also has the alias %). This is meant to operate in the pipeline, and it uses a? Process parameter that accepts a script block.
  • The ForEach scripting construct. This has a specific syntax, is not intended to be used in the pipeline, and does not have an alias.

Here’s all three in action, in a very simplistic example:

$names = @("Julie","Abby","Ben","Kevin")
$names | ForEach-Object -Process { Write-Output $_}

###The output
PS C:\Windows\system32> $names = @("Julie","Abby","Ben","Kevin")
PS C:\Windows\system32> $names | ForEach-Object -Process { Write-Output $_}
Julie
Abby
Ben
Kevin

The other type of foreach is a statement that once again processes each object in a collection and performs the code in the scriptblock for each item but this time a specific name is used to reference each item in the collection. An example is:

PS C:\Windows\system32> foreach ($name in $names) { Write-Output $name}
Julie
Abby
Ben
Kevin

The big difference is that, in the pipeline, ForEach-Object processes one object at a time. That means it can be slower, since that scriptblock must be interpreted on each iteration. It also tends to use less memory, since objects streaming down the pipeline one at a time don’t all have to be bunched up in a variable first.

The scripting construct tends to be faster, but it often has more memory overhead, because you have to give it the entire collection of objects at once, instead of streaming objects into it one at a time.

Both use vaguely similar syntax, but there are differences. It’s important to understand that they are not the same, and that they execute differently. It’s confusing because ForEach is both an alias and a scripting construct. PowerShell determine which you’re using by looking at the context in which you’re using it.

Let’s compare the execution time between ForEach vs ForEach-Object.

$Time1 = (Measure-Command {Get-ChildItem C:\Windows -Recurse | ForEach-Object {$_.Name}}).TotalMilliseconds
$Time2 = (Measure-Command {foreach ($file in (Get-ChildItem C:\Windows -Recurse)) {$file.Name}}).TotalMilliseconds
Write-Output "Time using ForEach-Object is $Time1"
Write-Output "Time using ForEach statement is $Time2"

The total time though is still faster with foreach statement (but would use more memory).

Time using ForEach-Object is 74523.1784
Time using ForEach statement is 47353.5139
ADVERTISEMENT
5/5 - (1 vote)
Previous Post

How to Enable the Computer Browser Service on Windows

Next Post

How to Enable or Disable File and Printer Sharing in Windows 10

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory