Introduction to PowerShell ForEach Object
ForEach-Object introduced in PowerShell in version 3.0. It used to perform repetitive tasks on the collection of input objects items. There various ways to create ForEach-Object they are given below.
- Script block: In Script block we can define or write our required operation. We can pass the variable to script block for object representations. See the below example for syntax.
Get-Process | ForEach-Object {process name}
- Operation statement: We can use operation statement for it, this way is almost similar to any other programming languages. The syntax of this is given below.
Get-Process | ForEach-Object <name of the process>
- Parallel script block: It introduced in PowerShell version 7.0. Here it allows us to run script block in parallel. Because of this block, we can write code which can be more time utilizing 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 mention below
-ArgumentList: It introduced in PowerShell version 3.0. It allows us to define or to pass an array of arguments to any function call. With the help, this command we can perform any big task in one go because we can pass all the items into array format to perform the task instead of passing one by one.
-AsJob: This introduced in PowerShell 7 version, so anyone using 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 child job details. These child jobs are the jobs of each parallel script blocks.
-Begin: This defines the script block which runs before any input processing by command. With the help of this, we can decide a few important things in this section before processing of input items.
4.7 (3,220 ratings)
View Course
-Confirm: This command is very important commands 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 command. This command allows us to perform something or to do something after all input processed by the command.
-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 that we are going to 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 introduced in PowerShell version 7.0
-RemainingScripts: It Defines those script blocks which are 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 at one time. It is an integer value. It will block Input till the current running script block count falls lower to ThrottleLimit.5 is it’s default number to run the process. This Introduced in PowerShell version 7.0.
-TimeoutSeconds: This command allows us to set some time to wait for all input process. Once we defined the timeout, all other running scripts will be stopped. And suppose there is any remaining input object which needs to be processed will be ignored. If we do not define the set timeout than it will consider it 0 seconds (default value).We can also stop parallel execution by using command Ctrl+C. One thing we need to remember here, we can not use -AsJob with –TimeoutSeconds.
-WhatIf: It displays results of the execution of the command before execution or without executing it, which will make you understand the consequences of execution of your command. It would be very helpful if you are 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. Now to perform the task we are using ForEach-Object command and adding 10 marks extra to each. Please see the example below along with screens.
$marks=22, 240, 238
$marks | ForEach-Object -Process {$_+10}
Output:
Example #2
In the below example we are trying to get all the modules that are installed on the current system PowerShell. 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 are writing a command which will print all the words of any sentence separated with space. We are splitting sentences separated with space.
"Ranjan Kumar pandey is a good boy" | ForEach-Object {$_.Split(" ")}
Output:
Example #4
Many times we will face a situation where we will be required to convert small case to uppercase and upper case to lower case. In such type of situation we can use ForEach-Object command and it takes a sentence an argument to command and command will process to the whole sentence and it will convert 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
From the above tutorial, we learned that we can perform any operation on objects either parallel or normally. It will reduce our extra effort to do some tasks on a repetitive basis.
Recommended Articles
This is a guide to PowerShell ForEach Object. Here we discuss the Parameters of PowerShell ForEach Object along with the respective examples. You can also refer to our other related articles to learn more –