The looping statements in Windows PowerShell include for, while, do/while, and foreach.
The for Loop
The for loop is a construct used to run commands in a statement block for as long as the specified condition evaluates to true. The for
loop is often used to iterate through an array or other type of
collection and run a set of commands against each of its elements.
Here is an example of a simple for loop:
PS > for($i = 1; $i -le 5; $i ++) { $i }
1
2
3
4
5
The example returns the value of $i as long as $i is less than or equal 5. Each time the for loop evaluates, the condition increments the value by 1.
The for loop is often used to loop through an array and run a command on each element in the array. Here is an example:
PS > $array = 1,2,3,4,5
PS > for($i = 0; $i -lt ($array.count); $i ++) { $array[$i] * 10 }
10
20
30
40
50
In this example, we first create an array holding the values 1 to 5. We then use the array in the for
loop to specify how many times the loop should run. Notice how we place
the array within parentheses in order to calculate the number of times
the for loop should run.
You can also use the for loop to iterate through a collection of sites, as shown in this example:
PS > $url = "http://SPServer01"
PS > $spWebs = Get-SPSite -Identity $url | Get-SPWeb
PS > for($i = 0; $i -lt $spWebs.Count; $i ++) {
>> $spWebs[$i] | Select-Object -Property Url, Created
>> }
Url Created
--- -------
http://spserver01 3/28/2010 11:44:11 PM
http://spserver01/Web1 4/10/2010 10:15:03 PM
http://spserver01/Web10 4/10/2010 10:15:37 PM
http://spserver01/Web2 4/10/2010 10:15:21 PM
http://spserver01/Web3 4/10/2010 10:15:23 PM
http://spserver01/Web4 4/10/2010 10:15:26 PM
http://spserver01/Web5 4/10/2010 10:15:28 PM
http://spserver01/Web6 4/10/2010 10:15:29 PM
http://spserver01/Web7 4/10/2010 10:15:31 PM
http://spserver01/Web8 4/10/2010 10:15:33 PM
http://spserver01/Web9 4/10/2010 10:15:35 PM'
Here, we use the Get-SPSite and Get-SPWeb cmdlets to retrieve all the sites in a specific site collection and store them in the $spWebs variable. First, we use the $spWebs variable in the test pipeline of the for loop to determine how many times the loop needs to run. Notice how we use the count
property to retrieve the number of sites. In the command block, we use
the same variable to return an array containing all sites, and then
retrieve each individual site in a new iteration of the loop, using the
index notation and the $i variable. Finally, we pipe the site object to
the Select-Object cmdlet and retrieve the Url and Created properties.
The do/while Loop
The while and do/while loops are language constructs used to run a command block as long as a condition evaluates to true.
Here is an example on a simple while loop:
PS > $i = 0
PS > while ($i -le 4) { "`$i = $i"; $i++ }
$i = 1
$i = 2
$i = 3
$i = 4
This example repeats the command block as long as the value of the $i
variable is not equal to 4. The variable is incremented in the code
block. We also use a backtick character (`) to comment away $i in the output. The backtick character is typically used to return variable names in the output.
The do/while loop is a variation of the while loop. In the while loop, the condition is checked in the beginning of the loop. In the do/while loop, the condition is checked in the end of the loop. Here is an example of a do/while loop:
PS > do { $i++; "`$i = $i";} while ($i -le 4)
$i = 1
$i = 2
$i = 3
$i = 4
$i = 5
In
this example, the loop increases by one as long as the variable is less
than or equal to 4. Notice how the value 5 is returned in the output.
This happens because the while condition is still true when the variable $i is equal to 5.
The foreach Loop
The foreach loop is a construct used to
iterate through a series of values in a collection of items. A block of
code contained within braces is used to execute a statement for each
item in the collection.
Here is an example of a basic foreach loop:
PS > $items = 1,2,3,4,5
PS > foreach($item in $items) { $item }
1
2
3
4
5
The example iterates through each element in the
array and performs the operation specified in the block of code on each
element.
You can also use the foreach loop when
working with SharePoint 2010. The example below demonstrates how to
iterate through items in a site collections recycle bin and display the
items Web, Title, and also display who deleted the item.
PS > foreach($i in (Get-SPSite http://SPServer01).RecycleBin) {
>> @{"Web"=$i.Web}
>> @{"Item"=$i.Title}
>> @{"DeletedBy" = $i.DeletedBy}
>> }
>>
Name Value
---- -----
Web Team Site
Item Tasks
DeletedBy POWERSHELL\sezel