For use when you need to run a script on several results (dozens) and performing it manually would be time-consuming.
Prerequisites
Before running a foreach
script, make sure you connect to any online services, if needed, for the commands in the foreach
block
Script
foreach
Starting the top of the script:
- We import the array from a CSV file with
Import-Csv
- We define a counter, which will allow us to show a progress bar
- We start running through the array, looking at each row in the table
- We increment our counter, to show what entry in the array we are on
- We show a progress bar with details on what is going on
- Activity is a text string shown to the user
- Current Operation shows the Row contents
- Percent complete shows how far we are
- We run any commands listed
- These steps are repeated until we run out of Rows in the Table
$Table = Import-Csv C:\temp\table.csv
$Counter = 0
ForEach ($Row in $Table) {
$Counter++
Write-Progress -Activity 'Processing details' -CurrentOperation $Row -PercentComplete (($Counter / $Table.count) * 100)
#Commands go here
}
ForEach-Object
ForEach-Object
is similar to foreach
but is designed to accept input piped to it. This allows you to run a command that returns an array, then you can do a task based on the items in the array.
Here is an example
Get-Mailbox -ResultSize:Unlimited | ForEach-Object {Write-Host $_.DisplayName}
The above command will get all mailboxes in the organization, then print out the display name for every mailbox. $_ is a shorthand for the data passed from the left side of the pipe ( | )