Certifications / Microsoft

How to Write a Simple Ping Test with PowerShell

How to Write a Simple Ping Test with PowerShell picture: A
Follow us
Updated on September 10, 2025

PowerShell is a very under-rated tool. This is despite the fact it has grown to be a powerful shell environment and strong scripting language. PowerShell can be used for a wide variety of IT tasks in every type of environment. One of those IT tasks is diagnosing why devices aren't connecting to a network or troubleshooting network issues. In these cases, ICMP packets are the tool of choice for IT pros.

Traditionally, the simple ping application in command prompt in Windows is the tool of choice for these types of tests. Ping is an easy way to shoot an ICMP packet to IP addresses on a network to see if there is a response or not. There is so much more information we can get from pinging a device, though. For instance, we could get the latency time for a response or the number of hops a packet needs to take to get to a device.

In this guide, you will learn how to write a simple but powerful ping test using PowerShell. We'll show you how to use the Test-Connection cmdlet for basic network checks, explore the more advanced Test-NetConnection for detailed diagnostics, and even show you how to automate your network monitoring with the Pester testing framework.

What is Pester?

Pester is a framework to help test and mock code within PowerShell. PowerShell isn't only a shell environment. PowerShell is also a scripting language. So, Pester is designed to help create tests for and write PowerShell scripts.

Pester can be useful for a lot of things. For this article, we will be using Pester to test whether the latency for a connection to a device is acceptable. We will also test whether a device or service is responding in an acceptable time frame.

Pester, along with native PowerShell applets, can be used to create a variety of scripts to test network functions and security with. For example, Pester could be used to create a test to ping every IP address on a specific subnet to see if any IP addresses are responding that shouldn't be.

How to Ping in PowerShell

Though pinging equipment on a network with PowerShell is as straightforward as pinging a device with Command Prompt, it's not hard. I'd wager to say that the only thing more difficult about using PowerShell is the number of characters you need to type to send a ping.

So, how do you ping in PowerShell? Use the 'Test-Connection' applet. It's that easy.

Much like you would use 'ping' in CMD, type in 'Test-Connection' followed by '-Ping', '-TargetName', and an IP address to send a ping to that device. The 'Test-Connection' applet will send four ICMP packets to that IP address and log their response times in the PowerShell console. It's that easy.

Of course, 'Test-Connection' has various commands that can be used with it, too. If you need help remembering those commands, type 'Help Test-Connection' into a PowerShell console. That will produce a list of commands that the 'Test-Connection' applet takes as well as the data type it will return.

That is an important thing to remember. PowerShell is different from other shell environments. PowerShell will return values based on various data types while other shell environments log information to the console as strings. If you are writing a script in PowerShell that compares values, you need to remember this. Otherwise, your scripts may not work properly.

There are a few commands that we need to focus on.

The first is the '-TargetName' command. Target name takes its parameters in the form of a string value. So, when you use this command, the IP address you are trying to ping will be read as a string in the 'Test-Connection' applet and not four different octet values. You can also pass an array of IP addresses with '-TargetName' if you want to send a ping to more than one device.

Another command is '-Count'. This command tells 'Test-Connection' how many ICMP packets should be sent to each device you are testing. By default, 'Test-Connection' sends four packets and listens for four responses. You may not want to run four tests, though. In this case, specify the number of times you want 'Test-Connection' to send an ICMP packet with that '-Count' property.

Finally, try to remember the '-TimeoutSeconds' property. By default, 'Test-Connection' will wait several seconds before timing out. If you are positive that your device should respond quicker, you can change the timeout value so that your tests complete faster.

How to Use Test-NetConnection (and When)

Test-Connection is a solid option for basic ICMP ping tests, but PowerShell offers an even more powerful cmdlet for network diagnostics called Test-NetConnection. It packs a lot under the hood and does a lot more than just simple pings. You can pull off detailed network connectivity testing along with TCP port testing for extra diagnostics in your commands 

Test-Connection vs Test-NetConnection

Feature

Test-Connection

Test-NetConnection

ICMP Ping

TCP Port Testing

Traceroute

Common Port Shortcuts

Detailed Diagnostics

Basic

Comprehensive

IPv6 Support

Some Basic Test-NetConnection Examples

Basic connectivity test with enhanced information:

Test-NetConnection -ComputerName google.com

Testing specific TCP ports:

# Test HTTPS connectivity
Test-NetConnection -ComputerName www.microsoft.com -Port 443
# Test RDP availability
Test-NetConnection -ComputerName server01 -Port 3389
# Test SQL Server connectivity
Test-NetConnection -ComputerName sqlserver01 -Port 1433

Common port shortcuts:

# Test HTTP connectivity
Test-NetConnection -ComputerName webserver01 -CommonTCPPort HTTP
# Test SMB file sharing
Test-NetConnection -ComputerName fileserver01 -CommonTCPPort SMB

Network path analysis with traceroute:

Test-NetConnection -ComputerName remote-server.com -TraceRoute

More advanced ping commands can be found in this GitHub Repo

An Overview of Writing Simple Ping Tests with PowerShell [VIDEO]

In this video, Trevor Sullivan covers how to write a simple ping test with the open-source Pester test framework. You will learn how to use the Pester PowerShell module, how to write a practical test within PowerShell, and how to complete a basic ping using the test connection command in PowerShell. 

Before you get started with the video, have ever been curious about where your IT career is headed? Be sure to check out the latest CBT Nuggets IT Training and Certification tools. Learn more about yourself with the IT Personality Assessment tool, or figure out what your next cert should be with the CBT Nuggets IT Certification Matrix.

How to Create a Ping Test with Pester in PowerShell

Creating a Pester test to ping devices on a network is easy. Before we dig into that, though, take a look at this GitHub repository.

You will find the example below in the '04-Write a Simple Ping Test' folder.

So, let's dig into this!

Describe  -Name "Home Lab" {
        Context -Name 'Ping tests' -Fixture {
It -Name 'Should ping default gateway' -Test {
[int](Test-Connection -Ping -ComputerName 10.0.0.1 -Count 1).Latency | Should -BeLessThan 3}
It -Name '10.0.0.77 should not be alive' -Test {
        [string](Test-Connection -Ping -ComputerName 10.0.0.77 -Count 1 -TimeoutSeconds 1). Status | Should -Be 'TimeOut'
}
}
}

In the above example, we can see that this case test is a simple Describe statement in Pester. We added a Context to this Describe statement as well. This could be useful if you want to perform other tests, like DNS tests, too, or if you want to separate Ping tests per device or subnet.

Inside of each of our contexts, we have two comparison tests. Each comparison test is denoted with the 'Lt' operator. The label for each comparison, or Assertion, is denoted by the '-name' property. When this test case is running, that label will be displayed in the console in PowerShell.

Finally, we have our test to run for each Assertion. First, we are telling the test that any values we get from the test command should be cast as an integer value. Next, we have the test command itself. This is where we use the 'Test-Connection' applet. For this test, we are only trying to compare the latency of the ICMP packet, so we only send one ICMP packet with the '-Count' property. We only need one response. After the test command is run, the value returned by that command is tested to see if it is less than 3. If it is, the test passes. Otherwise, it fails.

The second assertion is basically the same, but it tests whether a connection times out instead.

Frequently Asked Questions (FAQ)

How Do I Run a Continuous Ping in PowerShell?

PowerShell isn’t like the old ping -t command, it actually offers legit continuous monitoring:

# Simple continuous ping
while ($true) {
    Test-Connection -ComputerName google.com -Count 1
    Start-Sleep -Seconds 1
}
# Advanced continuous monitoring with statistics
$target = "google.com"
$successful = 0
$failed = 0
while ($true) {
    try {
        $result = Test-Connection -ComputerName $target -Count 1 -ErrorAction Stop
        $successful++
        Write-Host "$(Get-Date -Format 'HH:mm:ss') - $target: $($result.ResponseTime)ms (Success: $successful, Failed: $failed)" -ForegroundColor Green
    } catch {
        $failed++
        Write-Host "$(Get-Date -Format 'HH:mm:ss') - $target: Request timed out (Success: $successful, Failed: $failed)" -ForegroundColor Red
    }
    Start-Sleep -Seconds 1
}

What is the Difference Between ping.exe and Test-Connection?

  • Ping.exe sends back text output to the console and isn’t the best for scripting because your are limited to working with strings

  • Test-Connection gives us PowerShell objects with real properties which makes it fully scriptable and can integrate with PowerShell pipeline

How Can I Test if a Firewall Port is Open with PowerShell?

Use Test-NetConnection with the -Port parameter:

# Test specific port
Test-NetConnection -ComputerName server01 -Port 443
# Check if the connection succeeded
$portTest = Test-NetConnection -ComputerName server01 -Port 3389
if ($portTest.TcpTestSucceeded) {
    Write-Host "Port 3389 is open and accepting connections"
} else {
    Write-Host "Port 3389 is blocked or service is not running"
}

Why is My Test-Connection Script so Slow?

Most likely performance issues and solutions:

  • Your default timeout is too high: Use -TimeoutSeconds parameter for shorter timeouts

  • Testing too many hosts in a row: Try using PowerShell Jobs or ForEach-Object -Parallel instead

  • DNS resolution delays and issues: Wherever you can, try to use IP addresses instead of hostnames when possible. Hostnames don’t always resolve to your target IP, so if you have an IP then try use that

How Do I Ping IPv6 Addresses?

Both Test-Connection and Test-NetConnection support IPv6:

Test-Connection -ComputerName "2001:4860:4860::8888" -Count 1
Test-NetConnection -ComputerName "::1" -Port 80

Wrapping Up

Sending a ping through PowerShell is easy. Instead of using the 'Ping' command as we would in Command Prompt, we would use the 'Test-Connection' applet instead:

Test-Connection -Ping -TargetName 192.168.1.1

The Test-Connection applet collects tons of other useful information, too. For instance, we can get the latency of how long it took a packet to travel round trip or the status code of the ‘Test-Connection’ applet, too.

By understanding what data we can collect with the ‘Test-Connection’ applet, we can use Pester to create automated tests for us. These tests can be used for things like troubleshooting network issues or finding unwanted devices on a network. 

If you are the type of person that learns better from watching videos, check out CBT Nugget’s video on the same subject here


Download

By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.


Don't miss out!Get great content
delivered to your inbox.

By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.

Recommended Articles

Get CBT Nuggets IT training news and resources

I have read and understood the privacy policy and am able to consent to it.

© 2025 CBT Nuggets. All rights reserved.Terms | Privacy Policy | Accessibility | Sitemap | 2850 Crescent Avenue, Eugene, OR 97408 | 541-284-5522