PowerShell Genie

ForEach-Object

Use ForEach-Object to perform an operation on each item in a collection of input objects. See Microsoft.PowerShell.Utility module requirements and examples.

Command 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — ForEach-Object

Syntax

1PS> ForEach-Object [-Process] <ScriptBlock> [<CommonParameters>]
See the command anatomy

Inspect this authored example with fictional data in the bounded browser model. The numbered stages show how its objects change.

Get-Service | ForEach-Object { $_.DisplayName }
  1. Stage 1Get-ServicePositional values or no named parameters
  2. Stage 2ForEach-ObjectPositional values or no named parameters

    Try it

    Windows PowerShell

    PowerShell example · browser support is limited.

    Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

    Interactive simulation · fictional data

    Examples

    Run ForEach-Object

    1PS> 1 .. 5 | ForEach-Object { $_ * 2 }
    Show reference output

    Illustrative output · values vary by environment

    2
    4
    6
    8
    10

    List all with Get-Service

    1PS> Get-Service | ForEach-Object { $_.DisplayName }
    Show reference output

    Illustrative output · values vary by environment

    Background Intelligent Transfer Service
    DNS Client
    Windows Event Log
    Print Spooler
    Windows Time
    Windows Remote Management
    Windows Update

    Parameters

    Parameter definitions:

    • -Process

    ForEach-Object on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use ForEach-Object when you need to perform an operation on each item in a collection of input objects. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

    Common error and fix: A function-like script block returning incidental text can pollute the result. Keep only the transformed value on the success stream and use a diagnostic stream for explanatory messages. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

    See also: Measure-Object. Measure-Object calculates supported aggregates; ForEach-Object processes each input object with a script block. A count or sum is different from returning one transformed result per input. Read the linked command before substituting it; similar names do not imply identical inputs or output.

    ForEach-Object reference on Microsoft Learn

    Recap

    ForEach-Object Use ForEach-Object to perform an operation on each item in a collection of input objects. See Microsoft.PowerShell.Utility module requirements and examples.

    Next: Where-Object
    Genie guideForEach-Object

    Use ForEach-Object to perform an operation on each item in a collection of input objects. The console below is pre-loaded — run it, then change a value and run it again.