Development Tips, How To, Information, PowerShell, Tip & Tricks

An Introduction to PowerShell – Do While

Do While is the simplest of the looping constructs in PowerShell.

A looping construct is basically a piece of code that repeats the same action over and over again to a set of iteration, it loops through a set of statements, doing some action in each of its iteration, until some condition is met or that set of statement is exhausted.

Do While is simply a construct that says to PowerShell, “repeat set of things until some condition becomes true”.

For example, let’s set up a control variable called count and give it an initial value of one.

$count = 1

Then, let’s set up a simple Do While construct that adds 1 to whatever value of count is already in that variable, until the variable has the number 10 in it.

Do

{

$count = $count + 1

Write-Host “The current value of the variable is $count”

} While ($count –lt 10)

 

PS-10

Another way of doing same is:

You can also set up a Do While construct so that your set of commands only executes when the condition is true. You just need to eliminate the do statement, and only use while.

While ($count –lt 10)

{

$count = $count + 1

Write-Host “The current value of the variable is $count”

}

PS-11

Only difference in above both way of using Do While construct is in first way if we initialize Count with 11 then also the loop with execute once as the condition is tested after loop is executed at least once if condition is satisfied the loop will continue else will exit. Where as in second way the condition is tested before it enters the loop and will exit before first iteration of the loop.

PS-12

I will come up with more stuffs in my upcoming posts. 

Till then keep practicing and stay tuned for more details.

 

 

Advertisement
Development Tips, How To, Information, PowerShell, Tip & Tricks

An Introduction to PowerShell – If/Then

The simplest form of decision making in PowerShell is the if/then mechanism.

The code that runs if your comparison clause is YES/TRUE or NO/FALSE must be surrounded within curly braces, it is best practice to put these curly braces on lines by themselves so that you can match them up when you are writing more complicated scripts.

If condition is met then

{

Do This

}

Else

{

Do This

}

Example:

If (20 –gt 15)

{

Write-Host “20 is Greater than 15”

}

 

PS-7

Here the –gt is the PowerShell switch for “greater than”.

 

Another Example using Nesting

If (20 –gt 25)

{

Write-Host “20 is Greater than 25”

}

elseif (25 –gt 20)

{

Write-Host “25 is Greater than 20”

}

PS-8

You can include n number of these elseif blocks in your script, there is no maximum limit for how many times it occurs in script.

If (10 –gt 11)

{

Write-Host “10 is Greater than 11”

}

elseif (11 –lt 10)

{

Write-Host “11 is Less than 10”

}

elseif (20 –gt 40)

{

Write-Host “20 is Greater than 40”

}

else

{

Write-Host “None of above conditions are true”

}

PS-9

I will come up with more stuffs in my upcoming posts.

Till then keep practicing and stay tuned for more details.