Logic Examples

If

$Variable = 6
if ($Variable -gt 5 ) {
    Write-Host "Variable is greater than 5"
}
Variable is greater than 5

Else

$Variable = 4
if ($Variable -gt 5 ) {
    Write-Host "Variable is greater than 5"
} else {
    Write-Host "Variable is NOT greater than 5"
}
Variable is NOT greater than 5

Elseif

$Variable = 5
if ($Variable -gt 5 ) {
    Write-Host "Variable is greater than 5"
} elseif ($Variable -eq 5) {
    Write-Host "Variable is equal to 5"
} else {
    Write-Host "Variable is NOT greater than 5"
}
Variable is equal to 5

For

for ($i = 1; $i -le 5; $i++) {
    Write-Host "$i"
}
1
2
3
4
5

Foreach

$List = 1,2,3
foreach ($Number in $List) {
    Write-Host "$Number"
}
1
2
3

While

while ($Number -ne 5) {
    $Number++; Write-Host $Number
}
1
2
3
4
5

Do-While

$Place = 1
do {
    Write-Host "I'm #$Place!"
} while ($Place -ne 1)
I'm #1!