Need to confirm whether a port on your network is listening and accessible? In PowerShell, you can test open ports directly with minimal setup—just run a few commands to check if traffic can reach a specific service on a target host. This works for local machines, remote servers, or external services you need to connect to, and helps troubleshoot firewall blocking, network misconfigurations, or service failures.
What You'll Need
- A Windows PC running PowerShell (Windows 10/11 or Server versions).
- A target machine or service (e.g., a local host, remote IP, or web server).
- The port number you want to test—for example, 80 for HTTP, 443 for HTTPS, or custom ports like 3389 for RDP.
- Optional: A firewall management tool or command if you need to adjust rules afterward.
Step-by-Step
- Open PowerShell as Administrator
Right-click the PowerShell icon (or search for it) and select "Run as administrator." This ensures the commands have the necessary permissions to scan ports and interact with firewalls.
- Test if a Port is Open on a Local Machine
Use the following command to check if the default port (e.g., 80 for HTTP) is listening on your local machine:
Test-NetConnection -ComputerName localhost -Port 80Replace
80with the port you’re testing (e.g.,443for HTTPS). The output will show:- TcpTestSucceeded: True if the port is open.
- TcpTestSucceeded: False if it’s closed, blocked by a firewall, or the service isn’t running.
Example of a successful response:
Status Code: Connecting to localhost... Success TcpTestSucceeded : True - Test a Remote Port
For external or remote machines, use the command:
Test-NetConnection -ComputerName 192.168.1.100 -Port 22Replace192.168.1.100with the target IP. If the port is open, you’ll see a "Success" response; otherwise, it won’t.For a more detailed scan (e.g., checking multiple ports), use:
Test-NetConnection -ComputerName 192.168.1.100 -Port 22,80,443 - Check Port Status with Get-NetTCPConnection (Advanced)
To see all active listening ports on your local machine, run:
Get-NetTCPConnection -State ListenThis lists open ports along with their local IP/port pairs. Compare this with the port you’re testing to confirm its status.
Common Mistakes
- Running PowerShell without admin rights
If you get permission errors, PowerShell may not be able to probe ports correctly. Always run as admin for accurate results.
- Using the wrong port number
A common mistake is testing the wrong port—for example, testing port 80 (HTTP) on a machine that only runs HTTPS (port 443). Verify the correct port before testing.
- Ignoring firewall or antivirus blocking
Some firewalls (like Windows Defender Firewall) or third-party security tools may block port scans. Check firewall rules or disable them temporarily for testing (if safe).
- Assuming a port is open just because it appears in Get-NetTCPConnection
Some ports may be in a "Listen" state but still unreachable due to network misconfigurations (e.g., IP conflicts, incorrect routing). Test connectivity with
Test-NetConnectionfirst.
Tips
- Use -InformationLevel Detailed for verbose output
Add
-InformationLevel DetailedtoTest-NetConnectionto see additional details like round-trip time or ICMP errors. - Test multiple ports quickly
Use wildcards or loops (e.g., in a script) to test a range of ports efficiently. Example:
1..100 | ForEach-Object { Test-NetConnection -ComputerName localhost -Port $_ } - Check remote device reachability first
Before testing ports, verify the remote machine is reachable with:
Test-NetConnection -ComputerName 192.168.1.100If it fails, the issue is network-level (e.g., no route, firewall blocking ICMP). - Save results to a file
To log port test results, redirect output to a file:
Test-NetConnection -ComputerName 192.168.1.100 -Port 80 | Out-File -FilePath C:\PortTest_80.txt
Troubleshooting
- Port Test Fails: No Response
If
Test-NetConnectionreturnsTcpTestSucceeded: Falsebut the service is installed, check:- The service is running (e.g.,
Get-Service -Name httpdfor IIS). - Firewall rules allow traffic on that port (e.g.,
netsh advfirewall firewall add rule name="HTTP" dir=in action=allow protocol=TCP localport=80). - No conflicting IP addresses or routing issues between devices.
- The service is running (e.g.,
- Remote Host Unreachable
If the IP or hostname is unreachable, try:
- Check DNS resolution with
nslookup 192.168.1.100orping 192.168.1.100. - Verify the remote device is powered on and connected to the network.
- Check for VPN or proxy blocks that might isolate the host.
- Check DNS resolution with
- ICMP Blocked (No Ping Response)
If
pingfails butTest-NetConnectionworks, the firewall may block ICMP (ping). Test with:Test-NetConnection -ComputerName 192.168.1.100 -Port 80 -InformationLevel DetailedThe output will showICMPv4EchoRequestfailures if ICMP is blocked. - Port in Listen State but Still Failing
If
Get-NetTCPConnectionshows the port as "Listen" butTest-NetConnectionfails, the issue may be:- A misconfigured binding (e.g., IP address mismatch).
- The service is running but not actively accepting connections (e.g., a crashed process).
- IPv6 vs. IPv4 conflicts.
Restart the service (e.g.,
Restart-Service httpd) and retry.
Frequently Asked Questions
How do I test a port on a remote server through my firewall?
First, ensure your firewall allows inbound traffic on the target port. If the remote server’s firewall is blocking, add an exception for the port. Example (Windows Firewall):
netsh advfirewall firewall add rule name="RemotePort80" dir=in action=allow protocol=TCP localport=80
Then run the test again.
Why does Test-NetConnection return "Unreachable" when the port is technically open?
This often means the remote host isn’t resolvable by name (DNS issue), ICMP (ping) is blocked, or the network path is fragmented. Test with a raw IP (e.g., 192.168.1.100) instead of a hostname if DNS fails.
Can I use Test-NetConnection to check if a VPN is working?
Yes, but VPNs may block ICMP or restrict port testing. After connecting to the VPN, test a known open port (e.g., 80) on the VPN gateway’s IP. If it fails, the VPN may have connection limits.
What if the port is open but I still can’t connect?
Beyond port blocking, check:
- Authentication (e.g., username/password prompts for RDP).
- Service-specific constraints (e.g., database connections may require additional steps).
- Network encryption (e.g., TLS handshake failures on HTTPS).
telnet or Invoke-RestMethod for deeper diagnostics.
How do I test multiple ports at once efficiently?
Loop through a range of ports using PowerShell:
1..1000 | ForEach-Object { if (Test-NetConnection -ComputerName localhost -Port $_ -InformationLevel Quiet) { "Port $_ is open" } }
This logs open ports to the console.
Is there a way to test ports without admin rights?
No. PowerShell’s port scanning relies on elevated permissions for firewall interactions or SYN probes. If you lack admin rights, use third-party tools (e.g., Nmap) or ask for elevated access.