DaveWentzel.com            All Things Data

Branching and Looping Constructs

if
 
The following screenshot gives some examples.  Note there is no THEN and no END IF.  The ELSE is optional.  ELSEIF is used for secondary evaluations. 
 
 
 
Switch
 
Unlike IF which shortcircuits after the first true condition, switch will continue evaluating and will run every block that evaluates to true.  You can append BREAK to your code to override this behavior.  
 
 
Loops
while(1)
{
$a=something
if($a -eq 1) break;
if($a -eq 2)(continue)
#this line is not reached unless $a==1
}
 
do/while loop is very similar but ensures the loop is executed at least once. 
 
for(init;test condition;action)
{
     #a block of statements goes here
}
 
 
Foreach ($i in Get-Childitem c:\windows)
{$i.name; $i.creationtime}
 
Foreach ($i in Get-Childitem c:\windows)
{$i.name; $i.creationtime}
 
 

Add new comment