February 2019
Intermediate to advanced
626 pages
15h 51m
English
ForEach-Object is most often used as a loop (of sorts). For example, the following command works on each of the results from Get-Process in turn:
Get-Process | ForEach-Object {
Write-Host $_.Name -ForegroundColor Green
}
In the preceding example, a special variable, $_, is used to access each of the objects from the input pipeline. In the previous example, it is used to access each of the objects returned by the Get-Process command.
ForEach-Object may also be used to get a single property, or execute a single method on each of the objects.
For example, ForEach-Object may be used to return only the Path property when using Get-Process:
Get-Process | ForEach-Object Path
Or, ForEach-Object may be used to run the ...
Read now
Unlock full access