PowerShell offers a number of language constructs that allow you to manage the flow of your script and let you make decisions about what it should do. A few of the language constructs have conditionals, switches, loops, and variables.
Conditionals : This language construct is utilized to assess a conditional expression. If the conditional expression is true, a script block is accomplished:
if ( $i -eq 1)
{
## Do something
}
Else
{
## Do something
}
Switch : The switch statement lets you deliver a variable and a list of potential values. If the value matches the variable, then its script block is completed..
switch ($i) {
0
{
Write-Host “I is 0”
}
1
{
Write-Host “I is 0”
}
Default
{
Write-Host “I is 0”
}
}
Loops : What while statement does is repeat a block of code as long as the below mentioned conditional expression is working:
while ($i -eq 0) {
## do something
}
The do loop is identical to the while loop. The only distinction is PowerShell runs the do loop at the end of the loop.
do {
## do something
} while ($i -lt 0)
When you employ a foreach loop, PowerShell repeats the code for every item cited in the script.
$array = ( ‘item1’ , ‘item2’ , ‘item3’)
foreach ($item in $array) {
}
Make use of a for loop to execute statements constantly till a condition is met.
for ($i = 0; $i -lt 5; $i++)
{
$i
}