How to Write Conditional Statements in Windows PowerShell

Sometimes it is required to write conditional statements in Windows PowerShell. Windows PowerShell supports following two conditional constructs.

If Statements

To write an If statement use code similar to this:

$a = "white" 

if ($a -eq "red")
{"The color is red."}
elseif ($a -eq "white")
{"The color is white."}
else
{"The color is blue."}

Windows PowerShell If Statements

Instead of writing a series of If statements you can use a Switch statement, which is equivalent to VBScript’s Select Case statement:

$a = 2

switch ($a)

{
1 {"The color is red."}
2 {"The color is blue."}
3 {"The color is green."}
4 {"The color is yellow."}
default {"Other."}
}

Windows PowerShell Switch Statements

Related Articles


No comments yet

Leave a Reply





XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>