PowerShell loops

Overview

Powershell has these loop constructs:

  • while loop
  • do/while loop
  • do/until loop
  • for loop
  • Foreach loop

The following are brief examples of these loops.

while loop

$counter=0;
$limit=9;

Write-Host "";
Write-Host "variable counter=$counter";
Write-Host "variable limit=$limit";

while ($counter -le $limit)
{
 Write-Host "variable counter is now $counter";
 $counter++;
}

The loop doesn’t execute if variable counter is greater than variable limit.

do/while loop

$counter=0;
$limit=6;

Write-Host "";
Write-Host "variable counter=$counter";
Write-Host "variable limit=$limit";

do
{
 Write-Host "variable counter is now $counter";
 $counter++;
}
while ($counter -le $limit)

The loop executes at least once even if variable counter is greater than variable limit.

do/until loop

$counter=0;
$limit=6;

Write-Host "";
Write-Host "variable counter=$counter";
Write-Host "variable limit=$limit";

do
{
 Write-Host "variable counter is now $counter";
 $counter++;
}
until ($counter -gt $limit)

The loop executes at least once even if variable counter is greater than variable limit.

for loop

$limit=6;

Write-Host "";
Write-Host "variable limit=$limit";

for ($counter=4; $counter -le $limit; $counter++)
{
Write-Host "variable counter is now $counter";
}

or

$myArray = @(1, 2, 3, 4, 5, 99);

Write-Host "Array myArray has length" $myArray.Length;
for ($counter=0; $counter -lt $myArray.Length; $counter++)
{
   Write-Host $myArray[$counter];
}

In this example, we’re using a FOR loop to list the contents of an array. In both examples used, the loops won’t execute if variable counter is greater than variable limit or the length of the array given by myArray.Length.

foreach loops

foreach ($item in $myArray)
{
 Write-Host $item;
}

This FOREACH loop lists the contents of an array.

Foreach ($num in 1,3,5,7)
{
 Write-Host $num;
}

In this loop, the variable $num will hold the integer values 1, 3, 5 and 7.

or

Foreach ($num in 1..7)
{
 Write-Host $num;
}

The variable $num will contain the value 1 to 7 inclusive on each pass through the loop.

or

$myArray | ForEach-Object {Write-Host $_};

Another way of listing the contents of an array.

or

1..10 | ForEach-Object {
 $num = $_;
 Write-Host "num is now $num";
}

In this loop, the integer values 1 to 10 inclusive are used.

or

foreach ($line in Get-Content "C:\junk\testfile.txt") {Write-Host $line}

This loop can be used to read the contents of a text file.

See also

Powershell Loops

Using the Foreach-Object Cmdlet

Get-Content cmdlet

ForEach-Object cmdlet

about_Arrays

Advertisements
This entry was posted in powershell and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s