Powershell Day 5

Day 5 Programming in Powershell Part 3

Now as you know all information, in this blog we will look for some conditional and looping programming in Powershell. If you are programmer then you must be knowing how /what is conditional statement and looping statement like if …. Then… else and for/while loop. Powershell is also support them.

As Powerhsell is the object oriented programming, everything is object here. So object has

. Operator: where left is expand and right is expression.

PS C:\> “Powershell”.length

10

$ : for variable declaration

@ : to call function

:: Operator : is s static member accessor

: Operator: for global variable

PS C:\> $global:a=12

In earlier blogs we discussed about conditional operators eg. –gt, -lt

conditional statements and looping:

This  is somewhat similar as C programming.

if/elseif/else

if (<pipeline>) {<statementList>} elseif (<pipeline>) {<statementList>} else {<statementList>}

PS C:\> $a=10

PS C:\> if ($a -lt 5) {echo “value is less then 5” }

PS C:\> if ($a -lt 5) {echo “value is less then 5” } elseif

>> ($a -lt 9) {echo “Value is less then 9”} else

>> {echo “Value is greater then 9”}

Value is greater then 9

{} are mandatory

loop:

while Loop:

while ( <pipeline> ) { <statementList> }

PS C:\> $a=10

PS C:\> while ( $a -lt 20)

>> {echo $a;

>> $a++}

>> 

10

11

12

13

14

15

16

17

18

19

do/while loop

do { <statementList> } while ( <pipeline> )

PS C:\> $a=10

PS C:\> do {echo $a

>> $a++} while ($a -lt 20)

>> 

10

11

12

13

14

15

16

17

18

19

for loop:

for ( <pipeline> ; <pipeline> ; <pipeline> ) { <statementList> }

PS C:\> for ($i=0; $i -lt 10; $i++) {echo $i}

0

1

2

3

4

5

6

7

8

9

foreach loop:

foreach ( <variable> in <pipeline> ) { <statementList> }

PS C:\> foreach ($i in 1..10) {echo $i}

1

2

3

4

5

6

7

8

9

10

Foreach can be used with any command as well like

PS C:\> foreach ($i in dir *.txt) {echo $i}

label,break, and continue :this are similar command for c language.

Lable : to give lable to some statement

Break : break the loop

Continue: continue the loop

Switch :

 switch -options ( <pipeline> )

{

<pattern> { <statementList> }

<pattern> { <statementList> }

default { <statementList> }

}

PS C:\> switch (2) { 1 { “One” } 2 { “two” } default {“default”} }

two

PS C:\> switch (1) { 1 { “One” } 2 { “two” } default {“default”} }

One

PS C:\> switch (10) { 1 { “One” } 2 { “two” } default {“default”} }

default

Advertisements
This entry was posted in Powershell and tagged . Bookmark the permalink.

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