Powershell basics part 5 – Conditional statements and operators


Hello there and welcome to our powershell training materials. Today we will continue discovering other PS features. In this post I will focus on explaining the use of if and switch statements. As we discovered in a past article, these two are part of the so called reserved powershell words.
Now let’s start by explaining the if statement. If is used when you want to test a condition and if the condition is met then execute a piece of code, else execute another piece of code. I will write down the basic statement of if:

“if (condition1) {code 1}
elseif (condition2) {code 2}
elseif (condition3) {code 3}
elseif (condition4) {code 4}
.
.
else {code n}”
As you can see in this example, the condition1 is tested in the first line, if the test result is true then the code 1 is executed. If the test result is negative then the elseif condition2 is tested. Again if the result is true then code 2 is executed, if not then condition3 is tested and so on. At the end it’s only an else statement. That corresponds with the first if, condition1. The elseif statement is not necessary to exist, it depends on what problem we need to solve. A simple test condition can look as:

“if (condition1) {code 1}
else {code 2}”
The above code states that if the condition is meet execute code 1, if not execute code 2.\
The operators can be used in if statements starting with comparison or logical operators etc. I will give you a -eq example:

[int]$a = 14;
[long]$b=23;
if ($a -eq $b) {write-host true}
else {write-host False}
Save this example in a .ps1 file then execute it. You will see that it will display the message false. This happens because the value of $a is tested to see if it’s the same value as $b and because the values are different “false” is written.

The switch statement is used to test a condition over multiple values. The way switch works is just like the statement:

“if (condition1) {code 1}
elseif (condition2) {code 2}
elseif (condition3) {code 3}
elseif (condition4) {code 4}
.
.
else {code n}”

Switch looks something like this:

switch (expression)
{
value1 {code1}
value2 {code2}
value3 {code3}
.
.
default {coden}
}

This statement can be translated something like: test expression for value 1, if the condition is met execute code1, test the condition for value2 if the condition is met execute code 2 and so on. If no condition is met then the default coden is executed.

The operators are very close with the if and switch statements because usually in such statement you use a operator. Let;s type the following:

Operators Powershell
I have written under some of the most used operators in if and switch statements. You can find more on www.google.com by typing powershell operators or by checking the Get-Help command in powershell
Operator         Description
-eq – Equal to (case insensitive)
-ieq –  Equal to (case insensitive)
-ceq –  Equal to (case sensitive)
-ne –  Not equal to (case insensitive)
-ine –  Not equal to (case insensitive)
-cne –  Not equal to (case sensitive)
-gt –  Greater than (case insensitive)
-igt –  Greater than (case insensitive)
-cgt –  Greater than (case sensitive)
-ge –  Greater than or equal to (case insensitive)
-ige –  Greater than or equal to (case insensitive)
-cge –  Greater than or equal to (case sensitive)
-lt –  Less than (case insensitive)
-ilt –  Less than (case insensitive)
-clt –  Less than (case sensitive)
-le –  Less than or equal to (case insensitive)
-ile –  Less than or equal to (case insensitive)
-cle –  Less than or equal to (case sensitive)

–not – Not logical
! – Not logical
–and –  And logical
–or  – Or logical

This is all about the if and switch statements. In the next post I will talk more about loop statements. Thank you for reading this.
Have a nice day.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s