Skip to main content

Command referenceForEach-Object

ForEach-Object command (Windows / PowerShell)

WindowsPowerShellotherintermediate

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 / optionMeaning
-ProcessRun a script block for each input object.
-BeginRun initialization once before input.
-EndRun finalization once after input.
-ParallelProcess inputs concurrently in supported PowerShell versions.
-ThrottleLimitLimit 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 3

Runs 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.

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.