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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
## ForEach-Object ## [-InputObject ## [-Begin ## [-Process] ## [-End ## [-RemainingScripts ## [-MemberName] ## [-ArgumentList ## -Parallel ## [-ThrottleLimit ## [-TimeoutSeconds ## [-AsJob] ## [-WhatIf] ## [-Confirm] ## [ |
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:
1 2 3 4 5 6 7 8 9 10 |
########################################## ## PowerShell | Cmdlet | ForEach-Object ## ########################################## ## PowerShell Latest Version (5) ## loop through each object in powershell Get-Process | ForEach-Object ProcessName Get-Process | ForEach-Object -Process {$_.ProcessName} |
Loop Through Each Object Performing Some Calculation In PowerShell:
1 2 3 |
## loop through each object and perform some calculation for each object Get-Process | ForEach-Object -Process { Write-Host $_.Name " : " ($_.WorkingSet64/1MB) } |
Loop Through Each Object Performing Some Calculation At The Beginning In PowerShell:
1 2 3 4 5 |
## loop through each object and perform some calculation at the begining Get-Process | ForEach-Object ` -Begin { Write-Host "ProcessName : WorkingSetInMB"} ` -Process { Write-Host $_.Name " : " ($_.WorkingSet64/1MB) } |
Loop Through Each Object Performing Some Calculation At The End In PowerShell:
1 2 3 4 5 6 |
## loop through rach object and perform some calculation at the end Get-Process | ForEach-Object ` -Begin { $total = 0; Write-Host "ProcessName : WorkingSetInMB"} ` -Process { Write-Host $_.Name " : " ($_.WorkingSet64/1MB); $total += $_.WorkingSet64/1MB } ` -End { Write-Host "TotalMemoryUsageMB: $total"} |
Loop Through Each Object Based On Some Conditions In PowerShell:
1 2 3 4 5 6 |
## loop through each object based on some condition Get-Process | ForEach-Object ` -Begin { $total = 0; Write-Host "ProcessName : WorkingSetInMB"} ` -Process { if($_.Name -eq "chrome"){Write-Host $_.Name " : " ($_.WorkingSet64/1MB); $total += $_.WorkingSet64/1MB} } ` -End { Write-Host "TotalMemoryUsageMB: $total"} |
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