You are currently viewing PowerShell ForEach-Object Cmdlet

PowerShell ForEach-Object Cmdlet

PowerShell ForEach-Object Cmdlet

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed Where-Object cmdlet in PowerShell.

https://cloudaffaire.com/powershell-where-object-cmdlet/

In this blog post, we will discuss ForEach-Object cmdlet in PowerShell. You can use ForEach-Object cmdlet to loop through each object in a pipeline, performing some action for each object. ForEach-Object cmdlet provides a script block which is enclosed in parenthesis ‘{}’ where you can define some conditions or perform some action for each object in the pipeline. ForEach-Object script block has three components –

  • Begin: Executed only once at the beginning of the execution. You can initialize variables or print some message or perform any other actions in Begin block.
  • Process: Executed once for each object in the pipeline. You can perform your main action of script block in Process block.
  • End: Executed only once at the end of execution. You can print the summary of perform any other action in End block.

ForEach -Object Cmdlet Syntax:

ForEach -Object Cmdlet Argument List:

  • ArgumentList: Specifies an array of arguments to a method call.
  • AsJob: Causes the parallel invocation to run as a PowerShell job. A single job object is returned instead of output from the running script blocks.
  • Begin: Specifies a script block that runs before this cmdlet processes any input objects. This script block is only run once for the entire pipeline.
  • Confirm: Prompts you for confirmation before running the cmdlet.
  • End: Specifies a script block that runs after this cmdlet processes all input objects. This script block is only run once for the entire pipeline.
  • InputObject: Specifies the input objects. ForEach-Object runs the script block or operation statement on each input object.
  • MemberName: Specifies the property to get or the method to call.
  • Parallel: Specifies the script block to be used for parallel processing of input objects.
  • Process: Specifies the operation that is performed on each input object. This script block is run for every object in the pipeline.
  • RemainingScripts: Specifies all script blocks that are not taken by the Process parameter.
  • ThrottleLimit: Specifies the number of script blocks that in parallel. Input objects are blocked until the running script block count falls below the ThrottleLimit. The default value is 5.
  • TimeoutSeconds: Specifies the number of seconds to wait for all input to be processed in parallel. After the specified timeout time, all running scripts are stopped.
  • WhatIf: Shows what would happen if the cmdlet runs. The cmdlet is not run.

PowerShell ForEach -Object Cmdlet:

Loop Through Each Object In PowerShell:

Loop Through Each Object Performing Some Calculation In PowerShell:

Loop Through Each Object Performing Some Calculation At The Beginning In PowerShell:

Loop Through Each Object Performing Some Calculation At The End In PowerShell:

Loop Through Each Object Based On Some Conditions In PowerShell:

Hope you have enjoyed this article. In the next blog post, we will discuss the Measure-Object cmdlet in PowerShell.

To get more details on PowerShell, kindly follow below official documentation

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about?view=powershell-5.1

 

Leave a Reply