While/Until Powershell

Usage example for while and until in Powershell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# While
Clear-Host
$i = 1
while ($i -le 5)
{
  "`$i = $i"
  $i += 1
}


# While won't execute if condition is already true
Clear-Host
$i = 6
while ($i -le 5)
{
  "`$i = $i"
  $i = $i + 1
}


# Do
Clear-Host
$i = 1
do
{
  "`$i = $i"
  $i++
} while($i -le 5)


# Do will always execute at least once
Clear-Host
$i = 6
do
{
  "`$i = $i"
  $i++
} while($i -le 5)


# Use until to make the check more positive
Clear-Host
$i = 1
do
{
  "`$i = $i"
  $i++
} until($i -gt 5)