Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



PowerShell If Not ...

Introduction to Windows PowerShell's If -Not Logic

Once you have mastered the basic 'If' statement, you just need to aquire the nack of extending the logic to embrace the PowerShell 'If not' syntax.

Topics for PowerShell PowerShell's If -Not Statement

 ♣

Construction of the PowerShell 'If' Statement

Let us get started by studying PowerShell's 'If' logic.  The secret is to identify the two parts of the construction: (test condition) and {block command}.

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

If (condition) {Block Command}

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.

Example 1: Basic 'If' Statement

Think of this script as a preamble which checks whether the Alerter service is installed on your machine, and introduces the basic 'If' statement.   There is no -not comparison here, that comes in Example 2.

# PowerShell script to check if a service is installed
$SrvName = "Alerter"
$Service = Get-Service -display $SrvName -ErrorAction SilentlyContinue
If ($Service) {$SrvName + " is installed on this computer."}
Else {"No Alerter service indicates Windows 7 or Server 2008"}

Learning Points

Note 1:  The If (test) is simply this: can Get-Service find the $SrvName.  Try changing "Alerter" to "Print Spooler"

Note 2:  Avoid over-think; there is no 'Then' in a PowerShell 'If' statement. Furthermore, there is no endif in PowerShell as would be in VBScript.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft 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.

Download your free copy of WMI Monitor

Example 2: PowerShell If -Not Logic

The purpose of this script is to check for the Alerter service, the reason being Windows 7 machines no longer install this service.

Observe how -not reverses the 'If' logic compared with Example 1.

# PowerShell script to check if a service is -NOT installed
$SrvName = "Alerter"
$Service = Get-Service -display $SrvName -ErrorAction SilentlyContinue
If (-not $Service) {$SrvName + " is not installed on this computer."}
Else {"You probably have $SrvName, thus machine is Vista or W2K3"}

Note 3:  Other examples of If logic benefit from chaning -not to -ne meaning not equal.

Example 3: PowerShell If -ne

Here is a variation which employs -ne (not equal) rather than -not.

# PowerShell script to check if a service is not equal to ""
$SrvName = "Alerter"
$Service = Get-Service -display $SrvName -ErrorAction SilentlyContinue
If ($Service -ne "") {$SrvName + " is not installed on this computer."}
Else {"You probably have $SrvName, thus machine is Vista or W2K3"}

Note 4:  This is a crude and forced example of -ne, a better example for 'not equal' would be testing numeric data.

Example 3a: PowerShell If -ne Numeric Data

If (Test numbers not equal) {Script block}

$GuyNum = "12"
if($GuyNum -ne '7'){"Guy's number is not seven"}
Else {"Guy's number is $GuyNum "}

Note 5:  My biggest problem with not equal is wanting to write -neq, when I should be typing: -ne.  So often it's overthink that is my downfall!

Note 6:  While this a pretty crude example, it does plant the idea that you could use other comparators such as greater than -gt, or less than -lt.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

SolarWinds' Orion 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.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is 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 take advantage of SolarWinds' offer.

Download a free trial of the Network Performance Monitor.

Example 4: PowerShell If -Or

Following PowerShell's If -Not or If -ne there are more variations on the logic, for example '-Or'.

Clear-Host
$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

Note 6:  Do check the logic of your (test1 -or test2) statement.

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'

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Researching Comparison Operators

For More information refer to the built-in About_IF file

Clear-Host
Get-Help about_Comparison_Operators

Summary of PowerShell's If -Not 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 -Not, -ne, and also -or.

If you like this page then please share it with your friends

 


See more Windows PowerShell examples

PowerShell Home  • PowerShell If Statement  • PowerShell ElseIf  • PowerShell If -Not

PowerShell comparison operators  • PowerShell If -And  • PowerShell If -Or

Where Filter   • Loops  • Brackets  • -Match  • Switch  • Conditional Operators

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.

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.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: