Windows PowerShell


Windows PowerShell's If Statement

Introduction to Windows PowerShell's If Statement

PowerShell's 'If' statement comes under the umbrella of flow control.  Once you master the basic construction then you can increase the usefulness by adding, 'Else' and 'ElseIf' statements.

Topics for PowerShell PowerShell's If Statement

Construction of the 'If' Statement

As with so many PowerShell constructions, the type of bracket signifies how to break the script into sections.  It is worth emphasising that (parenthesis are for the first part, the condition), while {braces are for the block command}.

If (condition) {Do stuff}
or alternatively
If (test) {execute if true}

Example 1 Plain 'If'

$Number = 10
if ($Number -gt 0) {"Bigger than zero"}

Learning Points

Note 1:  Trace the construction and separate into: if (test) and {what to do}.

Note 2:  Avoid over-think; there is no 'Then' in PowerShell's 'If' statement. Instead of worrying about 'Then', pay close attention to the two types of bracket.

Note 3:  To double check your understanding, try amending, "Bigger than Zero" to a different text string, such as:  "Less than nought".  Once you have done that, set $Number to -1.

Example 1a File Content Example of Plain 'If'

PowerShell has a batch of help files.  One of these files contains help about the 'if' statement.   In the example below, $file references that file.  $Content is set to the content of the file.  The third line attempts to match a string to the contents of the file.

# Help on PowerShell's if statements
$file = "$PSHome\about_if.help.txt"
$Content = get-Content -path $File
if ($Content -match "The if Statement") {"We have the correct help file"}

Learning Points

This example is concerned with matching a string "The if Statement" to the contents of a file.

Example 2 'If' with 'Else'

# PowerShell's 'If' and 'Else' example
$file = "$PSHome\about_if.help.txt"
$Content = get-Content -path $File
if ($Content -match "The if Statement") {"We have the correct help file"}
Else {"The string is wrong"}

Learning Points

The best way to see how 'else' operates is to amend line 3 thus:
($Content -match "The ifzz Statement").

  ˚

Example 3 ElseIf

This example has a real task, and that is to check that we have the name of an actual file.

# Introducing PowerShell's ElseIf
$file = "$PSHome\about_if.help.txt"
$Content = get-Content -path $File
if ($Content -match "The if Statement")
{"Correct help file"}
ElseIf ($Content.Length -lt 1) {"Check file location"}
Else {"Content string is wrong"}

Learning Points

The advantage of ElseIf over plain Else, is that we can introduce a new test.  In the above example we use ElseIf to check if the length of the file is less than 1.  To activate the 'ElseIf' block, set $file to a non-existent file for example
$file = "$PSHome\about_ifzzz.help.txt".

If you have time, you could add more 'ElseIf' statements to cater for other eventualities.

Summary of PowerShell's If Construction

When it comes to filtering output, one of the oldest and best statements is the 'If' clause.  As usual, the secret of understanding the syntax is to pay close attention to the style bracket.  If (parenthesis for the test) and {braces for the action}.  Once you have mastered the basic 'If' statement, then extend your capabilities by researching 'Else' and 'ElseIf'.

Incidentally, the 'Vehicle' for our tests reveals a whole family of about_zyx files. My point is there is no command : 'get-help if'. However, if you look in the PowerShell directory then you will see 'About' files to assist with commands such as 'If' and 'ElseIf'.

See Also PowerShell Tutorials

PowerShell Home  • If Statement  • Conditional Operators  • Switch  • Loops  • Brackets

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.