Powershell - ForEach-Object Cmdlet
Advertisements
Cmdlet
ForEach-Object cmdlet can be used to perform operations on each object of a collection of objects.
In these examples, we're see the ForEach-Object cmdlet in action.
Example 1
In this example, we'll divide integer in an array. We'll refer to each object using $_.
1000,2000,3000 | ForEach-Object -Process {$_/1000}
Output
You can see following output in PowerShell console.
1 2 3
Example 2
Get the names of the items in current directory.
In this example, we'll split powershell module names.
"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | ForEach-Object {$_.Split(".")}
Output
You can see following output in PowerShell console.
Microsoft PowerShell Core Microsoft PowerShell Host
powershell_advanced_cmdlets.htm
Advertisements