Introduction to PowerShell’s Else Construction
PowerShell’s ‘Else’ is the safety valve for flow logic. Following the main 'If' statement you often you have multiple 'ElseIf' constructions, with a final plain ‘Else’ statement.
Topics for Windows PowerShell’s Else Statement
- Example 1: PowerShell ‘If’ On Its Own
- Example 2: PowerShell Else
- Example 3: PowerShell ElseIf and Plain Else
- Example 4: Check If a Process Is Running
♣
Refresher of PowerShell’s Plain ‘If’ Statement
Let us get started by reviewing PowerShell’s simple ‘If’ construction. In this instance you need just a (test), followed by a {block command}.
If (Test condition) {Block command payload}
Example 1: PowerShell ‘If’ On Its Own
# Simple PowerShell If statement
$Integer = 7
If ($Integer -gt 0) {"$Integer is a positive number"}
Learning Points
Note 1: Divide your If statements into two components: (test condition) and {what to do}. Observe each component in the above PowerShell construction.
Example 2: PowerShell Else
Let us extend my trivial ‘If’ example by introducing an ‘ElseIf’ statement. Incidentally, while ElseIf is not case sensitive, it does highlight the two words ‘Else’ and ‘if’.
Clear-Host
# PowerShell Else example
[System.Int32]$Integer = Read-Host "Enter Number"
If ($Integer -gt 0) {"$Integer is a positive number"}
Else {"$Integer is negative"}
Note 2: This script features ‘Read-Host’, the idea is to encourage you to test the logic by inputting different values.
Note 3: The art of mastering Else is to study the logic of each line, -gt is PowerShell's way of saying 'Greater than'.
Guy Recommends: Free WMI Monitor for PowerShell
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.
Download your free copy of WMI Monitor
Example 3: PowerShell ElseIf and Plain Else
It’s good practice to add a final Else statement. This catch-all will check for anything that slips through your ElseIf logic. For example, what if the value of $Integer was precisely zero?
# Simple PowerShell ElseIf Example
$Integer = -10 +10
If ($Integer -gt 0) {
"$Integer is bigger than zero"
}
ElseIf ($Integer -lt 0) {
"$Integer is negative"
}
Else {
"This number appears to be zero"
}
Note 4: I included the strange line: $Integer = -10 +10 to make it easy to test the script by deleting either -10 or +10.
Note 5: You many notice that unlike example 2, the above script produces the desired results without explicitly declaring the variable's type as [System.Int32].
Example 4: Check If a Process Is Running
Here is a simple example of PowerShell Else script, it checks if the Desktop Windows Manager is running.
# PowerShell script to investigate the Desktop Windows Manager.
Clear-Host
$ProcName = "DWM"
$Process = Get-Process -ErrorAction SilentlyContinue
If ($Process -Match "DWM") {$ProcName + " means Desktop Windows Manager runs."}
Else {"$ProcName no Desktop Windows Manager?"}
$Process
Note 6: Once If and Else get complicated it’s time to investigate PowerShell’s Switch parameter.
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
Researching PowerShell’s Else If Logic
For more information refer to PowerShell’s built-in About_If file
Get-Help About_If
DESCRIPTION of IF and ELSE
Use PowerShell’s If statement to run code blocks if a specified conditional test evaluates to true. Should the tests evaluate to false, you can specify an additional ELSE code block that is run.
Syntax
Here is an example to illustrate the command structure:
if (<test1>)
{<statement list 1>}
[ElseIf (<test2>)
{<statement list 2>}]
[else
{<statement list 3>}]
When you run an If statement, Windows evaluates the <test1> conditional expression as true or false.
If <test1> is true, <statement list 1> runs, and Windows PowerShell exits the If statement.
Should both <test1> and <test2> evaluate to false, then the code in <statement list 3> executes.
See more on PowerShell's ElseIf»
Summary of PowerShell’s Else Construction
PowerShell’s ‘Else’ is the catch-all for flow control logic. Often you have multiple ElseIf constructions, with a final plain ‘else’ statement.
If you like this page then please share it with your friends
See more Windows PowerShell flow control examples
• PowerShell Continue Statement • PowerShell If Statement • PowerShell ElseIf • PowerShell Else
• PowerShell Comparison Operators • PowerShell If -And • PowerShell If -Or • PowerShell If -Not
• Conditional Operators • Where Filter • PowerShell Real-life Techniques • PowerShell Home
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.