PowerShell Basics: If -Not Conditional Operator & Logic

PowerShell Basics_ If -Or Conditional Operator

Introduction to PowerShell’s If -Or Logic

The easiest way to understand PowerShell’s ‘If -or’ construction is to master the basic ‘If’ construction then add ‘-or’ to the test a second piece of logic.

Topics for PowerShell’s If -Or Conditional Operator

PowerShell’s Basic ‘If’ Statement

Let us get started by mastering PowerShell’s ‘If’ construction.  Focus on the two parts of the construction: (test) and {block command}.

It helps to keep one eye on PowerShell’s different types of bracket.  (The parenthesis style of bracket are used for the first part, namely the condition), while {braces are for the block command payload}.

If (condition) {Do stuff}

Here is a different explanation of the same ‘If’ flow control:

If (test) {execute when true}

Summary:  The PowerShell ‘If’ conditionally executes a block statement, providing the test expression is true.

PowerShell ‘If -or’ Statement

The only difference compared with the basic example is we have a second test introduced by -or.  The key point is that both tests are held between the same parentheses.

If (test1 -Or test2) {execute when true}

Example 1: Basic PowerShell ‘If’ Syntax

The purpose of this script is to check the underlying ‘If’ command.  Hour vehicle is the startup value for the Spooler service.  If it is set to Manual, then the script changes it to Automatic.

# PowerShell Script to change Startup to Automatic
$NameSrv = 'Spooler'
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv'"
If ($Service.Startmode -eq "Manual") {
Set-Service $NameSrv -startuptype Automatic }
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
$Service | Ft Name, Startmode -AutoSize

Learning Points

Note 1:  At the heart of this example is a test ($Service.Startmode -eq “Manual”) and a block command {Set-Service $NameSrv -startuptype Automatic }

Note 2:  Test the script by changing the startuptype for Spooler to Manual.  Either manually from the Services.msc or by running this PowerShell command:

Set-Service Spooler -startupType manual

Guy Recommends: Free WMI Monitor for PowerShell (FREE TOOL)Solarwinds 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 for PowerShell so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guesswork 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.

SolarWinds WMI Monitor Download 100% Free Tool

Example 2: PowerShell If -Or Conitional Operator

A scripting challenge occurs when you cater for the very real possibility that the service is set to ‘Manual’, or to ‘Disabled’.  You could solve this in PowerShell with ElseIf, alternatively, as in this example below, you could use -Or.

$NameSrv = 'Spooler'
If ($Service.Startmode -eq "Manual" -Or $Service.Startmode -eq "Disabled") {
Set-Service $NameSrv -startuptype Automatic }
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
$Service | Ft Name, Startmode -AutoSize

Note 3: Do check the logic of each line.  -eq is PowerShell’s way of saying equals; learn when to employ -eq and when to use the (=) sign.

Note 4: The benefit of employing -Or rather than ElseIf is writing tighter code.  This realization will prompt you to place the -Or inside the (test) parenthesis rather than create an extra line with a while new logical condition.

Example 3: PowerShell -Or Working Example

While this script is designed to test the tiny ‘-Or’ syntax, it has lots of extraneous code which changes the startupType to manual.  The sole purpose of this convoluted layout is so that you can change bits to double-check the logic and that you understand how -Or works in PowerShell.

$NameSrv = 'Spooler'
Set-Service $NameSrv -startupType manual
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv'"
$Service | Ft Name, Startmode
if ($Service.Startmode -eq "Manual" -Or $Service.Startmode -eq "Disabled") {
Set-Service $NameSrv -startuptype Automatic }
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
$Service | Ft Name, Startmode

Production script:

In real life you may want to strip the code down, and append a command to actually start the spooler service.

$Service = Get-WmiObject win32_service -filter "NAME = 'Spooler'"
if ($Service.Startmode -eq "Manual" -Or $Service.Startmode -eq "Disabled") {
Set-Service 'Spooler' -startuptype Automatic }
 Start-Service 'Spooler'

Note 5:  It’s always difficult to get the balance between example scripts that illustrate a point, and those that do useful work.

Guy Recommends:  Network Performance Monitor (FREE TRIAL)Review of Orion NPM v11.5

SolarWinds Network Performance Monitor (NPM) 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 on a 30-day free trial.

SolarWinds Network Performance Monitor Download 30-day FREE Trial

Multiple PowerShell -Or Conditions

Once your flow control works with one -Or, it’s relatively simple to append more -Or conditions.  Just remember that you only need one set of (parenthesis brackets).

Example 4: PowerShell If -And Script

PowerShell’s -And follows the same logic and construction as -Or

The purpose of this script is merely to test different dates and compare them with today’s date, which is held by the variable $Calendar.

$Calendar = Get-Date
If ($Calendar.day -eq '24' -And $Calendar.Month -eq '12') {"Christmas Day"}
ElseIf ($Calendar.day -eq '4' -And  $Calendar.Month -eq '7') {"4th of July"}
Else {"Today is not Christmas or the 4th of July"}

Note 4:  Pay close attention to the (first parenthesis).  In particular find the two tests .day -eq ’24’, also .Month -eq ’12.  My first point is they are separated by -And.  My second point is there is only one set of (condition brackets).

Note 5:  I included a few ElseIfs and a final Else statement to give the script a little more context and to give you ideas for your scripts.

Example 5: PowerShell ElseIf

While the PowerShell -or solution is more elegant, you could employ ElseIf instead.

$NameSrv = 'Spooler'
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv'"
if ($Service.Startmode -eq "Manual") {Set-Service $NameSrv -startuptype Automatic }
ElseIf ($Service.Startmode -eq "Disabled") {Set-Service $NameSrv -startuptype Automatic }
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
$Service | Ft Name, Startmode -auto

Researching PowerShell’s Else If Logic

For more information refer to PowerShell’s built-in About_IF file

Get-Help About_If

Get more help on PowerShell’s About_ files »

Summary of PowerShell’s If -Or Construction

Once you have mastered the basic If statement, you can extend its capabilities with If -Or.  This an alternative construction to ‘ElseIf’.  The secret of understanding PowerShell’s implementation of If -Or is to pay close attention to the brackets.  (test1 -or test2) then {braces for the script block}.

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.