Module 5: Looping and Logical Constructs: If..else
Module 5: Looping and Logical Constructs: If..else
If..else:
• IF.Else conditional constructs are used to execute script block based on condition
• If block executes, when condition true
• Else block executes, when condition is false
• ElseIf block executes, when previous condition false and current condition true
• IF. Else Syntax
If(Condition true)
{statement1;statement2 etc.}
Else
{statement1;statement2 etc.}
• IF.ElseIF Syntax
If(Condition1 true)
{Statemen1;Statement2 etc.}
ElseIf(Condition2 true)
{Statement3;Statement4 etc.}
Else
{Statement5;Statment6 etc.}
Examples:
Foreach-Object:
• ForEach-Object cmdlet executes block of statements for every single object in a pipeline
• $_ contains the current pipeline object
• "%" stands for ForEach-Object
While:
Statements in this block will execute at least once. Block will execute until the while
condition is true
It is an Exit control block.
Do
{
Write-host “$i”
}while($i –lt 10)
Do..Until:
Statements in this block will execute at least once. Block will execute until the until
condition is true
It is an Exit control block.
Do
{
Write-host “$i”
}until($i –lt 10)