Today, I thought I would share two important things used in Loops in PowerShell: ForEach-Object and ForEach. I will tell you the differences between these two.
Let us explore PowerShell ForEach-Object vs ForEach.
ForEach in PowerShell
The ForEach keyword in PowerShell is used to iterate over a collection of items. It is a control flow statement that allows you to execute a block of code for each item in a collection, such as an array or a list.
Syntax
Here is the syntax of ForEach.
foreach ($item in $collection) {
# Code to execute for each item
}Example
Here is a simple example of how to use foreach in PowerShell.
$numbers = 1..5
foreach ($number in $numbers) {
Write-Output $number
}In this example, the foreach loop iterates over the numbers from 1 to 5 and outputs each number.
Also, you can see the output in the screenshot below:

Characteristics
Here are some characteristics you should know why you should use ForEach loop.
- Memory Usage: The
ForEachloop loads the entire collection into memory before processing it. This can be efficient for small collections but may lead to memory issues with large datasets. - Performance: Generally faster for small to medium-sized collections since it processes items in memory.
- Readability: The ForEach loop is easy to read, making it a good choice for simple iterations.
Read PowerShell: Where-Object vs Select-Object
ForEach-Object in PowerShell
ForEach-Object is a cmdlet in PowerShell that processes objects as they come down the pipeline. It works with the pipeline, and it is more efficient for handling large datasets.
Syntax
Here is the syntax of ForEach-Object in PowerShell:
$collection | ForEach-Object {
# Code to execute for each item
}Example
Here is a simple example of how to use ForEach-Object in PowerShell.
1..5 | ForEach-Object {
Write-Output $_
}In this example, the ForEach-Object cmdlet processes each number from 1 to 5 as it comes down the pipeline and outputs each number.
I executed the above code, and you can see the output in the screenshot below:

Characteristics
Check out the characteristics below to understand why you should use ForEach-Object in PowerShell.
- Memory Usage: ForEach-Object processes one item at a time, which makes it more memory-efficient for large datasets.
- Performance: Can be slower compared to ForEach for small to medium-sized collections due to pipeline overhead, but it scales better with large datasets.
- Pipeline Integration: ForEach-Object is designed to work seamlessly with the PowerShell pipeline, making it ideal for processing streaming data or large collections that do not fit into memory.
Key Differences Between ForEach and ForEach-Object
1. Memory Usage
One of the primary differences between ForEach and ForEach-Object is how they handle memory.
ForEach loads the entire collection into memory before processing it, which can lead to high memory consumption for large datasets.
In contrast, ForEach-Object processes each item as it comes down the pipeline, making it more memory-efficient.
2. Performance
The performance of ForEach and ForEach-Object can vary depending on the size of the dataset.
For small to medium-sized collections, ForEach is generally faster because it processes items in memory.
However, for large datasets, ForEach-Object can be more efficient because it avoids the overhead of loading the entire collection into memory.
3. Use Cases
- ForEach: Best suited for small to medium-sized collections where memory usage is not a concern. It is also preferred for simple iterations due to its readability.
- ForEach-Object: Ideal for large datasets, streaming data, or scenarios where memory efficiency is crucial. It is also useful when working with the PowerShell pipeline.
ForEach-Object vs ForEach Examples
Let me show you a few examples of PowerShell ForEach-Object and ForEach.
Example 1: Process a Small Collection
Let me show you first how to process a small collection using both in PowerShell.
Using ForEach
$names = @('Alice', 'Bob', 'Charlie')
foreach ($name in $names) {
Write-Output "Hello, $name!"
}Using ForEach-Object
$names = @('Alice', 'Bob', 'Charlie')
$names | ForEach-Object {
Write-Output "Hello, $_!"
}In this example, both ForEach and ForEach-Object achieve the same result. However, ForEach is more readable and slightly faster for this small collection.
Example 2: Process a Large Collection
Let me show you how to process large collections using ForEach and ForEach-Object in PowerShell.
Using ForEach
$largeCollection = 1..1000000
foreach ($item in $largeCollection) {
# Process each item
}Using ForEach-Object
1..1000000 | ForEach-Object {
# Process each item
}For large collections, ForEach-Object It is more memory-efficient because it processes each item as it comes down the pipeline, avoiding loading the entire collection into memory.
Example 3: Work with the Pipeline
Here is an example of how to work with the pipeline using both PowerShell ForEach and ForEach-Object.
Using ForEach
Get-Process | ForEach {
Write-Output $_.Name
}Using ForEach-Object
Get-Process | ForEach-Object {
Write-Output $_.Name
}When working with the pipeline, ForEach-Object is the natural choice as it is designed to handle objects as they are streamed through the pipeline.
Read Filter Empty Values Using PowerShell Where-Object Cmdlet
PowerShell ForEach-Object vs ForEach – Summary
To summarize the differences and use cases of ForEach and ForEach-Object, here is a comparison table:
| Feature | ForEach | ForEach-Object |
|---|---|---|
| Memory Usage | Loads entire collection into memory | Processes one item at a time |
| Performance | Faster for small to medium collections | Scales better with large datasets |
| Readability | More readable for simple iterations | Slightly harder to debug |
| Pipeline Support | Not designed for pipeline | Designed for pipeline |
| Use Cases | Small to medium collections, simple loops | Large datasets, streaming data, pipeline |
Conclusion
I hope you know the difference between PowerShell ForEach-Object vs ForEach. You can use ForEach for small to medium-sized collections where memory is not a concern and readability is important. Opt for ForEach-Object when dealing with large datasets or when you need to work with the PowerShell pipeline.
Still, have questions for me? Leave a comment below.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.