Is It the Network? Test-Connection and Friends

When one computer can't reach another, four questions in order find where it breaks, each with its own command, Get-NetIPAddress for whether this PC has an address, Resolve-DnsName for whether the name turns into an address, Test-Connection for whether the computer answers, and Test-NetConnection -Port for whether the program's door is open.

Journeyman · PowerShell · Operating System · Updated

After this lesson you can

  • Check whether a computer answers with Test-Connection, and whether a port is open with Test-NetConnection -Port
  • Turn a name into an address with Resolve-DnsName, and read this PC's own addresses with Get-NetIPAddress

Key takeaways

  • Ask in order: does this PC have an address, does the name turn into one, does the computer answer, and is the right door open? The first no is where it breaks.
  • Get-NetIPAddress lists this PC's addresses. 127.0.0.1 always means this computer; an address that starts with 169.254 means it never got one from the router.
  • Resolve-DnsName turns a name into its IP address, the way a phone book turns a name into a number.
  • Test-Connection asks whether a computer answers at all, but many Windows PCs ignore it on purpose, so no answer isn't proof the PC is off.
  • Test-NetConnection -Port tests one door: 443 for websites, 445 for shared folders, 5985 for remoting, 22 for SSH.

"The internet's down." "I can't open the shared folder." "The printer's gone." Every office hears these, and every family hears them at home. Sometimes it really is the network; often it's one PC, one cable or one typo. Restarting things and hoping is what most people try, and it wastes a morning when it doesn't work. An IT help-desk tech asks a few questions in order instead, and the first question that gets a "no" is where the problem is.

Four questions, in order

Calling a friend works the same way. Does my phone have service? Do I have their number? Does their phone ring? Does the right person pick up? Each computer question has its own command:

  1. Does this PC have an address? Get-NetIPAddress
  2. Does the name turn into an address? Resolve-DnsName
  3. Does the computer answer? Test-Connection
  4. Is the right door open? Test-NetConnection -Port

All four only look; none of them changes anything. The examples ask about example.com, a website kept for examples like these, and about your own PC.

Does this PC have an address? Get-NetIPAddress

Every computer on a network has an IP address, its number there, like a phone number: four numbers with dots between them, such as 192.168.1.23 on a home network. Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress lists one row for each network adapter: your Wi-Fi or Ethernet with the address the router gave it, and Loopback Pseudo-Interface 1 with 127.0.0.1, the address that always means this computer, localhost. To see only that row:

Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias 'Loopback*' | Select-Object InterfaceAlias, IPAddress
InterfaceAlias              IPAddress
--------------              ---------
Loopback Pseudo-Interface 1 127.0.0.1

If your Wi-Fi row has an address that starts with 169.254, the PC asked the router for an address and got no reply: look at the Wi-Fi or the cable before anything else.

Does the name turn into an address? Resolve-DnsName

People use names; computers connect by number. DNS (Domain Name System) is the phone book that turns one into the other, and Resolve-DnsName looks a name up:

Resolve-DnsName -Name example.com -Type A
Name                                           Type   TTL   Section    IPAddress
----                                           ----   ---   -------    ---------
example.com                                    A      188   Answer     172.66.147.243
example.com                                    A      188   Answer     104.20.23.154

-Type A asks for the four-number kind of address; example.com has two. TTL is how many seconds the answer may be remembered, so yours will differ, and the addresses may too. A name that isn't in the phone book gives a red error: Resolve-DnsName -Name meeting-room.invalid says meeting-room.invalid : DNS name does not exist. That points at a typo, or a PC the network has never heard of. (Names that end in .invalid never exist, which makes them safe for examples.)

Does the computer answer? Test-Connection

Test-Connection sends a tiny "are you there?" message, a ping, and times the reply:

Test-Connection -TargetName example.com -Count 2 | Select-Object Ping, Address, Latency, Status
Ping Address        Latency  Status
---- -------        -------  ------
   1 172.66.147.243      20 Success
   2 172.66.147.243      17 Success

Latency is how long the reply took, in milliseconds. (Select-Object keeps the four columns worth reading; the full table also names your own PC.) Add -Quiet and the answer is one True or False, handy in a script. One catch: a Windows PC's firewall often blocks pings (turning on file sharing lets them through), so a silent office PC may be perfectly fine. That's why the last question matters most.

Is the right door open? Test-NetConnection -Port

The IP address finds the computer; a port finds the program on it. Think of an office building: the address gets you to the building, and the port is the door number of the office you want. Some doors worth knowing: 443 for websites (https), 445 for shared folders, 5985 for PowerShell remoting, 22 for SSH and 3389 for Remote Desktop.

Test-NetConnection -ComputerName example.com -Port 443 -InformationLevel Quiet
True

Now the remoting door on your own PC:

Test-NetConnection -ComputerName localhost -Port 5985
WARNING: TCP connect to (::1 : 5985) failed
WARNING: TCP connect to (127.0.0.1 : 5985) failed

ComputerName           : localhost
RemoteAddress          : ::1
RemotePort             : 5985
InterfaceAlias         : Loopback Pseudo-Interface 1
SourceAddress          : ::1
PingSucceeded          : True
PingReplyDetails (RTT) : 0 ms
TcpTestSucceeded       : False

::1 is localhost's other address, the newer, longer kind (IPv6). PingSucceeded is True, so the computer answers, but TcpTestSucceeded is False: nothing listens at door 5985, because remoting is off on this PC. On another PC, that's your answer for the "is it on?" question from PowerShell Remoting: Invoke-Command and Enter-PSSession.

On a Mac or Linux, Get-NetIPAddress, Resolve-DnsName and Test-NetConnection don't exist, but PowerShell 7's Test-Connection tests a door too: Test-Connection -TargetName example.com -TcpPort 443 says True. In Windows PowerShell 5.1, Test-Connection takes -ComputerName and has no -TcpPort, so use Test-NetConnection there.

Check yourself

2 questions to try on the lesson page.

So what

"It's the network" becomes four quick answers: an address, a name, a reply and an open door, and the first "no" tells you where to look.

Key terms

Cheat sheets

Related