Updated June 19, 2023
Introduction to PowerShell ForEach Object
ForEach-Object was introduced in PowerShell in version 3.0. It is used to perform repetitive tasks to collect input object items. There are various ways to create ForEach-Object they are given below.
- Script block: We can define or write our required operation in the Script block. We can pass the variable to the script block for object representations. See the below example for syntax.
Get-Process | ForEach-Object {process name}
- Operation statement: We can use an operation statement for it; this way is almost similar to any other programming language. The syntax of this is given below.
Get-Process | ForEach-Object <name of the process>
- Parallel script block: It was introduced in PowerShell version 7.0. Here it allows us to run script blocks in parallel. Because of this block, we can write code that can be utilized more in nature.
Syntax
1. ForEach-Object
[-InputObject <PSObject>]
[-Begin <script block which runs before processing any input>]
[-Process] <ScriptBlock[]>
[-End <script block which will runs after processing all inputs>]
[-RemainingScripts <script blocks which are yet to process>]
[-WhatIf<It display >]
[-Confirm<Before executing the command it will ask you for confirmation>]
[<CommonParameters>]
2. ForEach-Object
[-InputObject <PSObject>]
[-MemberName] <String>
[-ArgumentList <List of the argument object to process each item>]
[-WhatIf<prior information of result of execution>]
[-Confirm<Before executing the command it will ask you for confirmation>]
[<CommonParameters>]
3. ForEach-Object
-Parallel <scriptblock>
[-InputObject <PSObject>]
[-ThrottleLimit <int value of number of script block which can run at one time>]
[-TimeoutSeconds <int value of time out>]
[-AsJob<It allows us to invoke parallel jobs>]
[-WhatIf<prior information of result of execution>]
[-Confirm<Before executing the command it will ask you for confirmation>]
[<CommonParameters>]
Parameters
Here are the following parameters mentioned below
-ArgumentList: It was introduced in PowerShell version 3.0. It allows us to define or pass an array of arguments to any function call. With the help of this command, we can perform any big task in one go because we can pass all the items into an array format to perform the task instead of passing one by one.
-AsJob: This introduced in PowerShell 7 version, so anyone using it older than 7.0 can not be able to perform this operation. It allows us to invoke parallel jobs. It will return a job object instead of returning any output of executing the script block. And returned job contains the child’s job details. These child jobs are the jobs of each parallel script block.
-Begin: This defines the script block, which runs before any input processing by command. With the help of this, we can decide a few essential things in this section before processing input items.
-Confirm: This command is essential as it will display a confirmation box before running the command, and this confirmation box will ask for a Yes or No from your end. And command will execute according to input Yes or No from your end.
-End: This defines the script block, which will run after all input gets processed by the command. This command allows us to perform or do something after the command processes all input.
-InputObject: It defines the object for input to the command ForEach-Object. Our ForEach-Object command will run the script block for every object input.
-MemberName: It defines the property we will use to get the method call. These names can be anything like ProcessName property. This introduced PowerShell version 3.0.
-Parallel: Defines the script block for the parallel processing of input objects. It was introduced in PowerShell version 7.0
-RemainingScripts: It Defines those script blocks not considered yet by Process parameters. This command was introduced in PowerShell version 3.0.
-ThrottleLimit: It defines how many script blocks can be run simultaneously. It is an integer value. It will block input till the current running script block count falls lower to ThrottleLimit.5 is its default number for running the process. This is Introduced in PowerShell version 7.0.
-TimeoutSeconds: This command allows us to set some time to wait for all input processes. Once we define the timeout, all other running scripts will be stopped. And suppose any remaining input object that must be processed will be ignored. If we do not define the set timeout, it will consider it 0 seconds (default value). We can also stop parallel execution by using the command Ctrl+C. We must remember that we can not use -AsJob with –TimeoutSeconds.
-WhatIf: It displays the results of the execution of the command before execution or without executing it, which will make you understand the consequences of the execution of your command. It would be beneficial if you were running the command on the server or in any live environment, where you can see the outcome of your command before execution.
Examples of PowerShell ForEach Object
Learn the Examples of PowerShell ForEach Object.
Example #1
Suppose in any examination university decided to give 10 marks extra to every student. To perform the task, we use the ForEach-Object command and add 10 extra marks to each. Please see the example below, along with the screens.
$marks=22, 240, 238
$marks | ForEach-Object -Process {$_+10}
Output:
Example #2
In the example below, we are trying to install all the modules on the current PowerShell system. Here we are writing the command to get the name only. Please follow the below example along with the screen.
Get-Module -ListAvailable | ForEach-Object -MemberName Name
Output:
Example #3
In this example, we write a command to separate and print the words of any sentence with space. We are splitting sentences separated with space.
"Ranjan Kumar pandey is a good boy" | ForEach-Object {$_.Split(" ")}
Output:
Example #4
We often face a situation where we must convert small cases to uppercase and upper cases to lower cases. In such a situation, we can use the ForEach-Object command, it takes an argument to command, and the command will process to the whole sentence and convert the letter according to our requirement.
"Ranjan Kumar pandey is a good boy" | ForEach-Object {$_.ToUpper()}
Output :
"Ranjan Kumar pandey is a good boy" | ForEach-Object {$_.ToLower()}
Output :
Conclusion – PowerShell ForEach Object
The above tutorial taught us that we can perform any operation on objects parallel or normally. It will reduce our extra effort to do some tasks repetitively.
Recommended Articles
This is a guide to PowerShell ForEach Object. Here we discuss the PowerShell ForEach Object’s Parameters and the respective examples. You can also refer to our other related articles to learn more –