Pinging from PowerShell

This article represents using ping from PowerShell. PowerShell is a new command-line/scripting environment from Microsoft. Further there will be some other articles about using PowerShell in network administration.

How to ping single computer from PowerShell?

You can ping computer using Win32_PingStatus. The Win32_PingStatus WMI class represents the values returned by the standard ping command. More information on ping can be found in RFC 791.

Lets ping PC01 from PowerShell, execute this command:

Get-WmiObject -Class Win32_PingStatus -Filter "Address='PCO1'"

or in a brief notation

gwmi Win32_PingStatus -Filter "Address='PCO1'"

Command output:

__GENUS                        : 2
__CLASS                        : Win32_PingStatus
__SUPERCLASS                   :
__DYNASTY                      : Win32_PingStatus
__RELPATH                      : Win32_PingStatus.Address="localhost",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRoute="",SourceRouteType=0,Timeout=1000,TimestampRoute=0,TimeToLi

ve=128,TypeofService=128

__PROPERTY_COUNT               : 24
__DERIVATION                   : {}
__SERVER                       : LANTOOLBOX
__NAMESPACE                    : rootcimv2
__PATH                         : LANTOOLBOXrootcimv2:Win32_PingStatus.Addres
s="localhost",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRoute="",SourceRouteType=0,Timeout=1000,TimestampRoute=0,TimeToLive=128,TypeofService=128
Address                        : 127.0.0.1
BufferSize                     : 32
NoFragmentation                : False
PrimaryAddressResolutionStatus : 0
ProtocolAddress                : 127.0.0.1
ProtocolAddressResolved        :
RecordRoute                    : 0
ReplyInconsistency             : False
ReplySize                      : 32
ResolveAddressNames            : False
ResponseTime                   : 0
ResponseTimeToLive             : 64
RouteRecord                    :
RouteRecordResolved            :
SourceRoute                    :
SourceRouteType                : 0
StatusCode                     : 0
Timeout                        : 1000
TimeStampRecord                :
TimeStampRecordAddress         :
TimeStampRecordAddressResolved :
TimestampRoute                 : 0
TimeToLive                     : 128
TypeofService                  : 128

Address – Value of the address requested. The form of the value can be either the hostname (‘wxyz1234′) or IP address (’192.168.177.124′).

BufferSize – Buffer size sent with the ping command. The default value is 32.

NoFragmentation – If TRUE, ‘Do not Fragment’ is marked on packets sent. The default is FALSE, not fragmented.

PrimaryAddressResolutionStatus – Status of the address resolution process. If successful, the value is 0 (zero). Any other value indicates an unsuccessful address resolution.

ProtocolAddress – Address that the destination used to reply. The default is “”.

ProtocolAddressResolved – Resolved address corresponding to the ProtocolAddress property. The default is “”.

RecordRoute – How many hops should be recorded while the packet is in route. The default is 0 (zero).

ReplyInconsistency – Inconsistent reply data is reported.

ReplySize – Represents the size of the buffer returned.

ResolveAddressNames – Command resolves address names of output address values. The default is FALSE, which indicates no resolution.

ResponseTime – Time elapsed to handle the request.

ResponseTimeToLive – Time to live from the moment the request is received.

RouteRecord – Record of intermediate hops.

RouteRecordResolved – Resolved address that corresponds to the RouteRecord value.

SourceRoute – Comma-separated list of valid Source Routes. The default is “”.

SourceRouteType – Type of source route option to be used on the host list specified in the SourceRoute property. If a value outside of the ValueMap is specified, then 0 (zero) is assumed. The default is 0 (zero).

0 	None
1 	Loose Source Routing
2 	Strict Source Routing

StatusCode – Ping command status codes.

0 	Success
11001 	Buffer Too Small
11002 	Destination Net Unreachable
11003 	Destination Host Unreachable
11004 	Destination Protocol Unreachable
11005 	Destination Port Unreachable
11006 	No Resources
11007 	Bad Option
11008 	Hardware Error
11009 	Packet Too Big
11010 	Request Timed Out
11011 	Bad Request
11012 	Bad Route
11013 	TimeToLive Expired Transit
11014 	TimeToLive Expired Reassembly
11015 	Parameter Problem
11016 	Source Quench
11017 	Option Too Big
11018 	Bad Destination
11032 	Negotiating IPSEC
11050 	General Failure

Timeout – Time-out value in milliseconds. If a response is not received in this time, no response is assumed. The default is 1000 milliseconds.

TimeStampRecord – Record of timestamps for intermediate hops.

TimeStampRecordAddress – Intermediate hop that corresponds to the TimeStampRecord value.

TimeStampRecordAddressResolved – Resolved address that corresponds to the TimeStampRecordAddress value.

TimeStampRoute – How many hops should be recorded with timestamp information while the packet is in route. A timestamp is the number of milliseconds that have passed since midnight Universal Time (UT). If the time is not available in milliseconds or cannot be provided with respect to midnight UT, then any time may be inserted as a timestamp, provided the high order bit of the Timestamp property is set to 1 to indicate the use of a non-standard value. The default is 0 (zero).

TimeToLive – Life span of the ping packet in seconds. The value is treated as an upper limit. All routers must decrement this value by 1. When this value becomes 0 (zero), the packet is dropped by the router. The default value is 80 seconds. The hops between routers rarely take this amount of time.

TypeofService – Type of service is used. The default value is 0 (zero)

0 	Normal
2 	Minimize Monetary Cost
4 	Maximize Reliability
8 	Maximize Throughput
16 	Minimize Delay

If you want to change parameters of the command, e.g. BufferSize, Timeout and etc then use “Filter” parameter.

Get-WmiObject -Class Win32_PingStatus -Filter "Address='127.0.0.1' and BufferSize=64 and Timeout=3000"

To execute this command on your computer use ComputerName parameter. It should be ComputerName=. or ComputerName=localhost or ComputerName=127.0.0.1.

Get-WmiObject -Class Win32_PingStatus -Filter "Address='PC01'" -ComputerName .

If you want to execute ping command on the remote computer, you should define a name of the remote computer for ComputerName parameter.

For example this command executes on the PC02 computer and pings PC01 computer:

Get-WmiObject -Class Win32_PingStatus -Filter "Address='PC01'" -ComputerName PCO2

Format command output

A more useful form for summary information is to adjust display the Address, ResponseTime, and StatusCode properties:

Get-WmiObject -Class Win32_PingStatus -Filter "Address='127.0.0.1'" -ComputerName . | Select-Object -Property Address,ResponseTime,StatusCode

Result:

Address                                  ResponseTime                StatusCode
-------                                  ------------                ----------
127.0.0.1                                           0                         0

How to ping computers of entire subnet from PowerShell?

You can ping computers of entire subnet using PowerShell. We should use ForEach-Object to ping for each address:

"127.0.0.1","localhost","lantoolbox.com" | ForEach-Object -Process {Get-WmiObject -Class Win32_PingStatus -Filter ("Address='" + $_ + "'") -ComputerName .} | Select-Object -Property Address,ResponseTime,StatusCode

If you want to check your network that uses network number 192.168.1.0 and a standard Class C subnet mask (255.255.255.0) then use the following command.

1..254| ForEach-Object -Process {Get-WmiObject -Class Win32_PingStatus -Filter ("Address='192.168.1." + $_ + "'") -ComputerName .} | Select-Object -Property Address,ResponseTime,StatusCode

Related links

Jul 25, 2017

Similar articles