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 its usefulness by appending, 'Else' and 'ElseIf' statements.  Another alternative would be to use 'Switch'.

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 an alternative explanation would be
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.  My advice is that 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 = get-Help about_if
if ($File -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'

# Help on PowerShell's if statements
$File = get-Help about_if
if ($File -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:
($File -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.

# Help on PowerShell's if statements
$File = get-Help about_if
if ($File -match "The if Statement") {"We have the correct help file"}
ElseIf ($File.Length -lt 1) {"Check file location"}
Else {"File exists, but does not contain text string"}

Learning Points

Note 1:  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 = get-Help about_ifxx.

Note 2: To trigger the final 'Else', try changing:
$File = get-Help about_if
to
$File = get-Help about_scope

If you have time, you could add more 'ElseIf' statements to cater for other eventualities.  Alternatively, if the ElseIf construction is getting unwieldy, then try the superior switch command.

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

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', but there is a help file called 'about_if'.  Furthermore, if you look in the PowerShell directory then you will see 'About' files to assist with commands such as 'If' and 'ElseIf'.  You can list these 'About' files with the command:

get-help about*

See more Windows PowerShell examples

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

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

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

Please report a broken link, or an error.