PowerShell is a handy script tool for folks who manage systems and networks in Windows. This guide will show you how to check if a port is open on another device using PowerShell, which is a much safer bet than using Telnet. Telnet is an old-school network thing that’s not very secure anymore.
Also see: How to Install Telnet via CMD or PowerShell in Windows 11
Page Contents
Why use PowerShell to test port?
PowerShell is more than just a command-line tool. It’s made for managing systems and works with objects, not just text, which is awesome for tricky tasks.
Back in the day, when people needed to check if they could connect to a port on another computer, they mostly used Telnet. This is a way to talk to another computer over the network. But Telnet wasn’t very secure because it sent everything – even passwords – in text that anyone could read if they caught it in transit. Because of this, folks have moved away from using Telnet for safer options
Good news is, PowerShell has got you covered for a bunch of admin jobs, including checking out network issues. It’s got this super handy cmdlet called Test-NetConnection
. This tool not only helps you see if you can connect to a port but also gives you detailed info like how fast the connection is and the route your data takes. It’s a much safer and more powerful option than Telnet.
Relevant guide: How to Ping IPv6 Address in Windows 11
Testing port connection with PowerShell
The main command used for testing network connectivity via ports in PowerShell is the Test-NetConnection
cmdlet.
Test-NetConnection -ComputerName <HostName> -Port <PortNumber>
Where <HostName>
is the IP address or domain name of the computer you are trying to connect to, and <PortNumber>
is the number of the port on the <HostName>
that you are testing.
Example:
If you wanted to test if you can reach Google’s primary DNS server on port 53 to see if the port is open on that server, you would enter the following command:
Test-NetConnection -ComputerName 8.8.8.8 -Port 53
The output of the command provides a lot of useful information:
ComputerName : 8.8.8.8 RemoteAddress : 8.8.8.8 RemotePort : 53 InterfaceAlias : Ethernet SourceAddress : 192.168.0.2 TcpTestSucceeded : True
The TcpTestSucceeded : True
means the TCP connection to the remote server on the port has succeeded. If the connection failed, it would display TcpTestSucceeded : False
.
Related resources:
- How to Open an RDP Connection via CMD in Windows 11
- How to Remote Desktop Over The Internet in Windows 11
Testing multiple port connections
Checking lots of ports one by one can be an extremely tiring task. But, PowerShell can make it easier with a simple script:
$HostName = "<HostName>" $Ports = "<Port1>", "<Port2>", "<Port3>" foreach($Port in $Ports){ $Result = Test-NetConnection -ComputerName $HostName -Port $Port $Result | Format-List -Property ComputerName, RemoteAddress, RemotePort, TcpTestSucceeded }
Replace <HostName>
with the target hostname or IP address, and <Port1>
, <Port2>
, <Port3>
with the port numbers you wish to test. This script loops through each port, testing the connection and displaying the result.
Example:
In this example, we will use Google’s DNS server (8.8.8.8) as the host. We will check the server’s availability on three different ports: 53 (typically used for DNS), 80 (used for HTTP), and 443 (used for HTTPS). Our script will loop through each port, test the connection, and then output the results. Below is the specific PowerShell code used for this scenario.
$HostName = "8.8.8.8" $Ports = "53", "80", "443" foreach($Port in $Ports){ $Result = Test-NetConnection -ComputerName $HostName -Port $Port $Result | Format-List -Property ComputerName, RemoteAddress, RemotePort, TcpTestSucceeded }
Integrating with network troubleshooting
For advanced users, Test-NetConnection
can be a part of bigger scripts to help fix network issues. Imagine making a script that checks if you can connect to several servers and tells the network team by email if something’s not right.
With PowerShell, you can even make things fix themselves based on what you find out. Like, you could have a script that restarts a service if it sees that the port it’s supposed to be using is closed.
Can I test UDP port using PowerShell?
Yes, testing UDP ports with PowerShell is possible, but it’s a bit trickier and not as clear-cut as checking TCP ports. Why? Well, it’s all about how UDP works. Unlike TCP, UDP doesn’t really tell you if your data got through or in what order. This makes it tough to say for sure if a UDP port is open and ready for action.
Even though the Test-NetConnection
cmdlet in PowerShell doesn’t directly do UDP checks, you can try using other cmdlets like Test-Connection
as a sort of workaround. Just remember, Test-Connection is more about checking if you can reach a place on the network using ICMP, not UDP, so it’s more of a basic “are you there?” check than a true UDP port test. If you need something more tailored to UDP, you might have to look into other tools or maybe even write your own script that uses .NET magic to send UDP packets and see what happens.
Final notes
PowerShell is really handy for making network admin tasks a lot simpler. Using the Test-NetConnection cmdlet, you can say goodbye to old and not-so-secure methods like Telnet for checking if ports are open. PowerShell lets you get into the nitty-gritty of network diagnostics, automate the boring stuff, and manage lots of connections without breaking a sweat.
Just a heads-up: always make sure you’re testing networks and systems you’re actually allowed to. It’s important to keep things above board and respect privacy and legal boundaries when poking around with network tests.