PowerShell: Network Connection Testing
"The Internet Is Broken" Is Not A Diagnosis
When a machine cannot reach something, the useful question is where the chain breaks. Local stack? Gateway? DNS? The far end?
A script that tests outward from the machine, in order, and stops at the first failure, turns a vague complaint into one failing hop. That is the whole idea, and it takes about thirty lines.

The Order That Mirrors How Traffic Flows
Test in this sequence and the first failure is your answer:
- Loopback —
127.0.0.1and::1. Fails only if the TCP/IP stack itself is broken. - Own IP — the machine's LAN address. Fails if the adapter is up but not bound correctly.
- Default gateway — the router. Fails if the cable, switch port, Wi-Fi association or DHCP is the problem.
- A public IP —
8.8.8.8or1.1.1.1. Fails if the gateway is fine but the path outward is not. - DNS resolution — resolve a name. Fails if pings work but lookups do not, which is the classic "connected but no internet".
- A public name — resolve and reach, confirming the full path.
Each step isolates one layer. That is the difference between this and a single ping google.com.
The Script
function Test-NetworkConnection {
<#
.SYNOPSIS
Tests connectivity outward from this machine and reports the first failure.
.NOTES
Author: Luke Keam - techgeek.biz
Free for personal and commercial use, keep this reference.
#>
param(
[string]$Gateway,
[string]$PublicIP = '1.1.1.1',
[string]$TestName = 'www.google.com'
)
if (-not $Gateway) {
$Gateway = (Get-NetRoute -DestinationPrefix '0.0.0.0/0' |
Sort-Object RouteMetric |
Select-Object -First 1).NextHop
}
$steps = @(
@{ Name = 'Loopback IPv4'; Test = { Test-Connection 127.0.0.1 -Count 1 -Quiet } }
@{ Name = 'Loopback IPv6'; Test = { Test-Connection ::1 -Count 1 -Quiet } }
@{ Name = 'Local IP'; Test = { Test-Connection $env:COMPUTERNAME -Count 1 -Quiet } }
@{ Name = "Gateway ($Gateway)"; Test = { Test-Connection $Gateway -Count 1 -Quiet } }
@{ Name = "Public IP ($PublicIP)"; Test = { Test-Connection $PublicIP -Count 1 -Quiet } }
@{ Name = "DNS ($TestName)"; Test = { Resolve-DnsName $TestName -ErrorAction Stop | Out-Null; $true } }
@{ Name = "Reach ($TestName)"; Test = { Test-Connection $TestName -Count 1 -Quiet } }
)
foreach ($step in $steps) {
$ok = & $step.Test
if ($ok) {
Write-Host (" OK " + $step.Name) -ForegroundColor Green
} else {
Write-Host (" FAIL " + $step.Name) -ForegroundColor Red
Write-Host "First failure found - settings below for review:" -ForegroundColor Yellow
ipconfig /all
return
}
}
Write-Host "All connectivity tests passed." -ForegroundColor Green
}
Run it bare and it discovers the gateway itself:
Test-NetworkConnection
What Each Failure Means
Loopback fails. The TCP/IP stack is not functioning. This is rare, and it means a repair or reset, not a network fix.
Local IP fails but loopback works. The adapter has no valid address, or the address is wrong for the subnet. Check DHCP.
Gateway fails, public IP fails. The problem is at or before the router. Check the cable and the switch port first — a duplex mismatch or a dead port looks exactly like this.
Gateway works, public IP fails. The LAN is fine and the path out is broken. Router WAN, ISP, or a firewall rule blocking ICMP.
Pings work, DNS fails. This is the most common "internet is down" that is not. The machine has connectivity but no working resolver. Resolve-DnsName will show it immediately, and ipconfig /flushdns or a different DNS server fixes it.
Two Notes On The Tools
Test-Connection is ping; Test-NetConnection is more. The first sends ICMP. Many networks block ICMP, so a failure there does not prove the host is unreachable. When you need to test a service, use the port-aware cmdlet:
Test-NetConnection -ComputerName fileserver -Port 445
That performs a real TCP connect and tells you whether the listener is answering, which is what you usually care about when "the share is down".
Resolve-DnsName is the resolver test, and it distinguishes "lookup returned nothing" from "lookup failed". Use it rather than nslookup, which has its own resolver and can succeed where the system resolver fails.


Making It Useful Day To Day
Run it before you start troubleshooting and again after the fix. It gives you a consistent record of where the chain broke, and it stops the "have you tried rebooting the router" conversation because you can say exactly which hop failed.
The gateway auto-detection is worth keeping even if you hardcode everything else — on a machine that changes networks (laptop between office and home) hardcoding the router address is how the script starts reporting false failures.
Don't trust the download button. Trust the hash.
If this was useful, More hands-on tech guides at TechGeek. No pressure — read it whenever it's useful.