PowerShell Test-Connection: Ping Hosts and Check Connectivity

PowerShell Test-Connection: Ping Hosts and Check Connectivity

PowerShell Tips Editor 4 min read
PowerShell Test-Connection: Ping Hosts and Check Connectivity

You need to know instantly whether a server is reachable — or if it’s the network that’s the problem. PowerShell ping Test-Connection gives you that answer as a proper object, not a wall of text you have to parse. Whether you’re verifying connectivity before a deployment, loop-monitoring a flapping host, or generating a reachability report across 50 devices, Test-Connection and its sibling Test-NetConnection handle all of it without leaving PowerShell.

Basic Ping with Test-Connection

Test-Connection sends ICMP echo requests — exactly what ping.exe does, but the output is structured objects you can pipe, filter, and export. By default it sends four pings and displays round-trip time and status for each.

Test-Connection -ComputerName google.com
Source        Destination     IPV4Address      Bytes    Time(ms)
------        -----------     -----------      -----    --------
WORKSTATION   google.com      142.250.80.46    32       11
WORKSTATION   google.com      142.250.80.46    32       12

To limit to a single ping and control the count, use -Count:

Test-Connection -ComputerName 192.168.1.1 -Count 1

Quiet Mode for True/False Result

When you use the result inside an if statement, you want a Boolean, not an object. The -Quiet switch returns $true if at least one ping succeeds and $false otherwise. Without -Quiet, a failed ping throws an error rather than returning $false.

if (Test-Connection -ComputerName server01 -Count 1 -Quiet) {
    Write-Output "server01 is reachable"
} else {
    Write-Output "server01 is NOT reachable"
}

Ping Multiple Hosts from a List

Feed a list of hostnames to Test-Connection and capture each result as a custom object. The cleanest pattern is to loop through an array or a text file and build a status table.

$servers = @("server01", "server02", "192.168.1.10", "google.com")

$results = foreach ($server in $servers) {
    [PSCustomObject]@{
        Host      = $server
        Reachable = (Test-Connection -ComputerName $server -Count 1 -Quiet)
    }
}

$results | Format-Table -AutoSize
Host          Reachable
----          ---------
server01      True
server02      False
192.168.1.10  True
google.com    True

Test-NetConnection for Port Checks

Test-NetConnection extends connectivity testing to TCP ports — something plain ICMP ping cannot do. Use it when ICMP is blocked by a firewall but you still need to verify that a service is listening. The -Port parameter performs a TCP handshake and reports TcpTestSucceeded.

# Check if HTTPS (443) is open on a host
Test-NetConnection -ComputerName server01 -Port 443
ComputerName     : server01
RemoteAddress    : 10.0.0.5
RemotePort       : 443
InterfaceAlias   : Ethernet
SourceAddress    : 10.0.0.2
TcpTestSucceeded : True

Retry Until Host Responds

During a reboot cycle you may want to loop until a host comes back online. Combine Test-Connection -Quiet with a while loop and a short sleep to build a simple wait-for-online utility.

$target = "server01"
Write-Output "Waiting for $target to come online..."

while (-not (Test-Connection -ComputerName $target -Count 1 -Quiet)) {
    Start-Sleep -Seconds 10
    Write-Output "Still waiting..."
}

Write-Output "$target is back online at $(Get-Date)"

Export Reachability Report

Combine multiple checks and export to CSV for a quick network health snapshot you can share with the team or store for trend analysis.

$servers = Get-Content -Path "C:\Logs\servers.txt"

$report = foreach ($s in $servers) {
    [PSCustomObject]@{
        Timestamp = (Get-Date -Format "yyyy-MM-dd HH:mm:ss")
        Host      = $s
        Reachable = (Test-Connection -ComputerName $s -Count 1 -Quiet)
    }
}

$report | Export-Csv -Path "C:\Logs\reachability-$(Get-Date -Format yyyyMMdd).csv" -NoTypeInformation
Write-Output "Report saved."

Common Errors and Fixes

  • ICMP blocked by firewall: Many corporate firewalls block ping. Test-Connection will return $false or throw an error even when the host is fully operational. Switch to Test-NetConnection -Port 80 or -Port 443 to verify TCP connectivity instead. This gives a far more reliable answer when ICMP is filtered.
  • Quiet flag needed in if statements: Without -Quiet, a failed ping writes an error to the error stream rather than returning $false. Always use -Quiet when the result drives conditional logic. Combine it with -ErrorAction SilentlyContinue to suppress error output entirely.

Related Cmdlets / See Also

Wrapping Up

Test-Connection with -Quiet and Test-NetConnection with -Port cover almost every connectivity check scenario you’ll encounter. For your next step, combine the multi-host loop above with a scheduled task to run hourly and email the report when any host is unreachable — a simple but highly effective monitoring solution.

Send-Item -To