Command referenceForEach-Object
ForEach-Object command (Windows / PowerShell)
What it does
Run a script block for each object arriving through the pipeline. Guides covering the foreach-object powershell usually need pipeline-friendly examples, common parameters, and how the cmdlet compares with classic CMD or Linux tools. This page includes syntax, options, and copyable PowerShell examples.
Run a script block for each object arriving through the pipeline.
Syntax
ForEach-Object [-Process] <ScriptBlock[]> [-Begin <ScriptBlock>] [-End <ScriptBlock>] | ForEach-Object -Parallel <ScriptBlock>
Common options
| Flag / option | Meaning |
|---|---|
| -Process | Run a script block for each input object. |
| -Begin | Run initialization once before input. |
| -End | Run finalization once after input. |
| -Parallel | Process inputs concurrently in supported PowerShell versions. |
| -ThrottleLimit | Limit parallel runspaces. |
Practical examples
Project names
Get-Process | ForEach-Object { $_.Name }Emits each process name.
Rename files
Get-ChildItem *.txt | ForEach-Object { Rename-Item $_ "$($_.BaseName).bak" }Renames each selected file.
Parallel checks
1..5 | ForEach-Object -Parallel { Test-Connection server$_ -Count 1 } -ThrottleLimit 3Runs bounded concurrent checks.
Common mistakes
- Using ForEach-Object where direct property projection is clearer and faster.
- Starting excessive parallel work without a suitable throttle limit.
Related commands
- Where-ObjectFilter pipeline objects by property values or a script block predicate.
- Get-ChildItemEnumerate items and child items in filesystem, registry, and other providers.
- Get-CommandDiscover commands, aliases, functions, scripts, and applications available to PowerShell.
- Get-HelpDisplay syntax, parameter details, examples, and conceptual PowerShell help.
- manRead installed manual pages for commands, formats, and APIs.
- historyDisplay or modify the current shell's command history.
- clsClear visible text from the Command Prompt screen.
FAQ
What does ForEach-Object do in PowerShell?
Run a script block for each object arriving through the pipeline.
How can I see examples for ForEach-Object?
Run `Get-Help ForEach-Object -Examples`, then adapt an example to the provider and objects you are using.
What is a practical ForEach-Object example?
Try `Get-Process | ForEach-Object { $_.Name }` — Emits each process name.
Does ForEach-Object return text or objects?
ForEach-Object normally participates in PowerShell's object pipeline; formatting seen in the console is a view of those objects.
Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.