July 2017
Intermediate to advanced
648 pages
31h 9m
English
The foreach statement is a language construct used to iterate through values in a collection of items. The following example shows the syntax used to loop through a collection of mailboxes, returning only the name of each mailbox:
foreach ($mailbox in Get-Mailbox) {$mailbox.Name}
In addition, you can take advantage of the PowerShell pipeline and perform loop processing using the ForEach-Object cmdlet. This example produces the same result as the one shown previously:
Get-Mailbox | ForEach-Object {$_.Name}
You will often see the given command written using an alias of the ForEach-Object cmdlet, such as the percent sign (%):
Get-Mailbox | %{$_.Name}
Read now
Unlock full access