Powershell For Loop


this way you can iterate list/collection using for.

Example

for ($i = 0; $i -lt $myLists.Count; $i++)
{
  $list = $myLists[$i];
  # ...
}

Copy and Try it


this way you can iterate collection using foreach.

Example

$collection = @(1, 2, 3, 4, 5)
foreach ($i in $collection)
{
Write-Host $i
}

Copy and Try it

Putting the lines in a single string

Example

$lines = ''
for ($i=0; $i -lt 5; $i++)
{
$lines += "The current value of i is $i`n"
}
$lines

Copy and Try it