PowerShell’s Win32_PingStatus

Scripting With PowerShell’s Win32_PingStatus

There are occasions when it’s useful to know whether a computer is online, or whether it’s unavailable.  Ping is the classic command to test the status of a ComputerName or IP address.  WMI (Windows Management Instrumentation) provides a class called Win32_PingStatus, which we are going to put to work with PowerShell’s Get-WmiObject.

Topics for PowerShell Win32_PingStatus


Real Life Tasks

The rationale behind Win32_PingStatus is that it can give you a handle on whether or not a machine is available on the network.  If a particular machine is running, then the rest of our PowerShell script can ‘do stuff’.  That ‘stuff’ could be saving files, or opening eventlogs.  By using a construction such as: if StatusCode = zero, then go ahead, else try another machine; you can avoid your script failing just because the target machine is offline.

Most real life tasks are multi-dimensional.  In this instance PowerShell collects TCP/IP information such as ping status values with Win32_PingStatus.  The next stage is to develop PowerShell constructions, such as Do…Until loop, and also remember the syntax for the parent cmdlet Get-WmiObject.  As usual the secret of success is to break down the task in to manageable chunks.  The bonus of this approach is that you can understand what is going on, and thus adjust my example scripts to suit your circumstances.

Example 1 – List the WMI Classes

The idea behind this basic script is merely to research possible WMI classes, and to gain experience with the vital command: Get-WmiObject.  My hidden agenda is to explain how I knew there was a class called Win32_PingStatus.

# PowerShell script to list the Win32 classes
Get-WmiObject -List | where {$_.name -Match "Ping"}

Note 1:  The benefit of using -Match rather than -Like or -eq is that we only need a partial match.  It is also easy to  modify the code to search for other Win32 classes.  For example amend to: Get-WmiObject -List | where {$_.name -Match "Network"}

Guy Recommends: The Free IP Address Tracker (IPAT) Free IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.  For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. 

Download the Free IP Address Tracker

Example 2 – PowerShell Win32_PingStatus

Compared with other Win32 classes, Win32_PingStatus is unresponsive to help and to Get-Member.  My breakthrough came when I discovered the -f, or -filter parameter; for example -filter "Address=’IP’ " or even -filter "Address=’IP’ ".

# PowerShell Win32_PingStatus to test the status of an IP address
$AddrIP = "Address=’′ "
Get-WmiObject Win32_PingStatus -filter $AddrIP

Note 0: While not strictly necessary, I introduced the variable $AddrIp to highlight the need for you modify this IP address to suit your network.

Note 1: The key construction is the "Address=".  Pay close attention to the speech marks.  Double speech marks around the "whole address", single speech marks around the ‘IP’ portion.  Remember to introduce "Address" with -f or the full word, -filter.

Note 2: Many people prefer the alias gwmi instead of Get-WmiObject.

Note 3:  In the resulting output concentrates on StatusCode.  For once, zero is good news, it means that Ping has been successful and found the IP address.  On the other hand, a binary value of 11010 means that ping failed.

Challenge: Substitute a hostname for the IP address.

Example 3 – What To Do With StatusCode

In example 3 we get down to business, and ping a whole range of IP addresses.  For this we need a PowerShell loop.  There are dozens of ways of achieving this, I happen to have chosen Do…Until.  I would like to emphasise that my goal was to test the following range of IP addresses to   If you don’t use this IP range then please amend my line: $IpAdr = "192.168.1." to the network address of your local subnet.  The safest, but most tedious way of checking all the IP address is to increase $i -eq 20 to $i -eq 254.

# PowerShell ping computer script
$i =1
$AddrIP = "192.168.1."
Do { $Ip4th = $AddrIP+ $i
$Pingy = Get-WmiObject Win32_PingStatus -f "Address=’$Ip4th’"
$Pingy | Format-Table Address, StatusCode -hideTableHeaders -auto; $i++
until ($i -eq 20)

Results: A StatusCode of 0 means the machine responded to ping.  Other values such as 1101, or 11003 mean that ping could not find that particular IP address.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 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

Example 4 – PowerShell Script to Test Which IP Addresses are Online

The most important feature of example 4 is to recognise where the script could deliver a payload, or ‘do stuff’.  If you want a subroutine which wrote to a share on the machine, or extracted it’s eventlogs then replace the contents of the {brackets} after the ‘if’ and ‘else’ commands with your ‘stuff’.

# PowerShell Win32_PingStatus Example
$i =1
$Ip = "192.168.1."
Write-Host "IP Address"
Write-Host "—————————————-"
Do { $Ip4th = $Ip + $i
$Pingy = Get-WmiObject Win32_PingStatus -f "Address=’$Ip4th’"
if($Pingy.StatusCode -eq 0) {
     "{0,0} {1,5} {2,5}" -f
     $Pingy.Address, $Pingy.StatusCode," ON NETWORK"}
     {"{0,0} {1,5} {2,5}" -f $Pingy.Address, $Pingy.StatusCode, " xxxxxxxxx"
until ($i -eq 20)

Note 1: Much of this script is cosmetic, and represents my thought processes in creating a script which detects which IP addresses are ‘ON NETWORK’.  You could write much tighter code.

Note 2: "{0,0} {1,5} {2,5}" -f is to format, or to space the output.  The first number is the item of each pair (0,1,2), the second is the tabbing or padding (0,5,5).  Feel free to adjust the second number in each pair.

Note 3: One of the most important parts of the script is the ‘if’ statement:
if($Pingy.StatusCode -eq 0).  What this does is to test if we have any StatusCode responses of zero, if so we assume that that IP Address in online.

Guy Recommends: SolarWinds Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

Download your free copy of SolarWinds Wake-On-LAN

Summary of PowerShell Win32_PingStatus

With a PowerShell ping computer script you can check the status of the machine before your main script delivers its payload.  The feature of these examples is Win32_PingStatus, and the way it checks the StatusCode value.  From a pure PowerShell point of view this script also demonstrates the Do…Until loop and the -f formatting technique.  My advice is to use this script as a template, to which you add a payload that ‘does stuff’ at the remote machine.

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


See More Microsoft PowerShell WMI Examples:

Home   • PowerShell Get-WmiObject   • Win32_ComputerSystem   • Free WMI Monitor

WMI Class  • [WMI] Type  • Win32_printer   • Win32_product   • SystemRestore   • WMI Services

WMI Disk   • DNS   • Memory  • PowerShell -Filter   • Check Server UpTime   • ConvertToDateTime

Please email me if you have a script examples. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.