PowerShell is a valuable scripting tool in the toolkit of system and network administrators in Windows OS. This article aims to provide an in-depth guide on how to use PowerShell to test port connections to see if a port is open on a remove device, without relying on Telnet, an older network protocol with known security issues.
Also see: How to Install Telnet via CMD or PowerShell in Windows 11
Page Contents
Why use PowerShell to test port?
PowerShell is a task-based command-line shell and scripting language built on .NET. It’s designed especially for system administration. Unlike most shells, PowerShell interprets objects—structured information—and not just text. This can be tremendously powerful when it comes to complex data manipulation and system administration tasks.
In the past, testing port connections was primarily performed using Telnet, a network protocol that allows a user to communicate with a remote device. However, due to its unencrypted nature and security vulnerabilities, primarily its transmission of data in plain text, including passwords, Telnet has been largely deprecated in favor of more secure protocols.
Fortunately, PowerShell can be used to perform several administrative tasks, including network diagnostics. PowerShell provides a much more secure and powerful way to test port connections using the Test-NetConnection
cmdlet. This cmdlet displays diagnostic information for a connection, including robust data on network latency and routing paths.
Relevant guide: How to Ping IPv6 Address in Windows 11
Testing port connection with PowerShell
The core 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
Testing multiple ports can be cumbersome if done one by one. Luckily, PowerShell is well-equipped to handle this task too. Here’s how you can check multiple ports using 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 integrated into larger scripts for network troubleshooting. For example, you could create a script that tests connection to a list of servers and emails the network team if any fail.
PowerShell can also be used to automate remediation actions based on the results. For example, you might have a script that restarts a service if it finds the port it should be listening on is not open.
Can I test UDP port using PowerShell?
Yes, you can test UDP ports using PowerShell, but it is more complex and less definitive compared to TCP port testing. The inherent challenge lies in the nature of UDP itself – it’s a connectionless protocol, meaning it doesn’t provide feedback about packet delivery or order. This makes it harder to confirm if a UDP port is open and responsive.
While the Test-NetConnection
cmdlet doesn’t natively support UDP testing, you can use other cmdlets, such as Test-Connection
, as a workaround. Keep in mind that Test-Connection
uses ICMP, not UDP, which can provide a basic reachability test but not a definitive UDP port test. For a more specific UDP test, you may need to use different tools or create a custom script that leverages .NET methods to create and send UDP packets.
Concluding thoughts
PowerShell is a powerful scripting language that can greatly simplify network administration tasks. The Test-NetConnection
cmdlet is a robust tool that enables you to test port connections without having to rely on outdated and insecure tools like Telnet. With PowerShell, you can easily perform network diagnostics, automate tasks, and handle multiple connections efficiently and effectively.
Always be mindful of the legal and ethical considerations when conducting network testing, and only perform tests on networks and systems where you have explicit permission.