- PowerShell Tutorial
- PowerShell - Home
- PowerShell - Overview
- PowerShell - Environment Setup
- PowerShell - Cmdlets
- PowerShell - Files and Folders
- PowerShell - Dates and Timers
- PowerShell - Files I/O
- PowerShell - Advanced Cmdlets
- PowerShell - Scripting
- PowerShell - Special Variables
- PowerShell - Operators
- PowerShell - Looping
- PowerShell - Conditions
- PowerShell - Array
- PowerShell - Hashtables
- PowerShell - Regex
- PowerShell - Backtick
- PowerShell - Brackets
- PowerShell - Alias
- PowerShell Useful Resources
- PowerShell - Quick Guide
- PowerShell - Useful Resources
- PowerShell - Discussion
Powershell - ForEach-Object Cmdlet
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
To Continue Learning Please Login
Login with Google