DEV Community

terminalpuncher.com
terminalpuncher.com

Posted on

Popular Conditional Statements Made Simple in Powershell

# "FOR" Statement
for ($a = 1; $a -le 10; $a++) {$a}

# "DO While" Loop
$a = 1
do {$a; $a++} 
while ($a -lt 10)

# "DO until" Loop
$a = 1
do {$a; $a++} 
until ($a gt 10)

# "IF" statement
 $a = ""white"" 
 if ($a -eq ""red"") {""The color is red.""} 
 elseif ($a -eq ""white"") {""The color is white.""}  
 else {""The color is blue.""}

# "Select Case" statement
$a = 2
switch ($a) 
 { 1 {""The color is red.""} 
   2 {""The color is blue.""} 
   3 {""The color is green.""} 
   4 {""The color is yellow.""} 
   default {""Other.""}
 }
Enter fullscreen mode Exit fullscreen mode

Discussion (0)