How to Perform Ping Tests with PowerShell?

A powerful Test-Connection PowerShell cmdlet is available as a replacement for the ping command in Windows.

The Test-Connection command allows you to send ICMP echo requests packets to one or more remote hosts and receive echo response replies.

To check the availability of a remote host via ping ICMP, run the command:

Test-Connection theitbros.com

ping powershell

ADVERTISEMENT

You can use pipe with the Select-Object cmdlet to select only the required properties in the command results:

Test-Connection theitbros.com| Select-Object Address, IPV4Address, ResponseTime

powershell ping test

List all possible attributes of a System.Management.ManagementObject#root\cimv2\Win32_PingStatus object using the command:

Test-Connection theitbros.com -Count 1|Get-Member|select name, MemberType

powershell test ping

Name MemberType

—- ———-

PSComputerName AliasProperty

Address Property

BufferSize Property

NoFragmentation Property

PrimaryAddressResolutionStatus Property

ProtocolAddress Property

ProtocolAddressResolved Property

RecordRoute Property

ReplyInconsistency Property

ReplySize Property

ResolveAddressNames Property

ResponseTime Property

ResponseTimeToLive Property

RouteRecord Property

RouteRecordResolved Property

SourceRoute Property

SourceRouteType Property

StatusCode Property

Timeout Property

TimeStampRecord Property

TimeStampRecordAddress Property

TimeStampRecordAddressResolved Property

TimestampRoute Property

TimeToLive Property

TypeofService Property

__CLASS Property

__DERIVATION Property

__DYNASTY Property

__GENUS Property

__NAMESPACE Property

__PATH Property

__PROPERTY_COUNT Property

__RELPATH Property

__SERVER Property

__SUPERCLASS Property

ConvertFromDateTime ScriptMethod

ConvertToDateTime ScriptMethod

IPV4Address ScriptProperty

IPV6Address ScriptProperty

You can ping to multiple hosts at once:

Test-Connection theitbros.com,google.com

By default, the cmdlet sends 4 ICMP packets. You can perform a ping check with a single packet:

Test-Connection theitbros.com -Count 2

You can change the delay (in seconds) between sending packets and the buffer size (in bytes):

Test-Connection theitbros.com -Delay 4 -BufferSize 128

With the -Repeat option, you can send ping requests continuously. Press CTRL+C to interrupt ping test:

Test-Connection 1.1.1.1 -Repeat

ping test powershell

In PowerShell 7.x, you can use the -MtuSize attribute to get the path MTU size:

Test-Connection -TargetName theitbros.com –MtuSize

test ping powershell

ADVERTISEMENT

You can run Test-Connection as a PowerShell background job. For example, you want to ping the list of computers in the text file CheckServers.txt in the background:

$pingjob = Start-Job -ScriptBlock { Test-Connection -TargetName (Get-Content -Path "c:\ps\CheckServers.txt") }

$Results = Receive-Job $pingjob -Wait

With the -Traceroute option (available in PowerShell Core 6.x+), you can trace a path to a remote host:

Test-Connection theitbros.com -Traceroute

One of the interesting features of the Test-Connection cmdlet is that it allows you to ping from remote computers. For example, you want to check host availability from three servers in different locations:

Test-Connection -Source "lon-app1", "par-man01”, "tw-man02" -ComputerName theitbros.com

You can also use another PowerShell cmdlet to check host availability using ICMP. Run the command:

Test-NetConnection -ComputerName theitbros.com

The Test-NetConnection cmdlet is primarily used to check the open TCP ports. But it also checks the availability of the host using ICMP ping. If an ICMP response is received from a remote host, the following line will appear in the command results:

PingSucceeded : True

PingReplyDetails (RTT) : 96 ms

ping test script powershell

Testing the availability of remote computers using ICMP Ping is useful in PowerShell scripts if you need to perform some action. For example, you run PowerShell script on remote computer only if one of the pings sent to the computer succeeds:

ADVERTISEMENT
$servername="tw-man02"

If ((Test-NetConnection $servername -WarningAction SilentlyContinue).PingSucceeded -eq $true)

{

Invoke-Command -ComputerName $servername -ScriptBlock {Restart-Service spooler}

}
I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.