The ForEach-Object command

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 ToString method on a set of dates:

PS> (Get-Date '01/01/2019'), (Get-Date '01/01/2020') | ForEach-Object ToString('yyyyMMdd')
20190101

20200101
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.133.228