PowerShell Check If Port Is Open

PowerShell check if port is open

The following commands are used to determine if the specified port is open on the remote computer, but we can also use them on the local computer. To use them on the local machine, we must replace the example.com with localhost. Note that the specified port in the below commands may or may not be open on your local computer, so it is better to use the netstat -a command to see all the listening and established connections. Then, you can choose one of those ports to test the following commands.

NOTE: We have hidden the value of SourceAddress due to privacy concerns; you should see your local machine’s IP address there.

Using Test-NetConnection Cmdlet

Use the Test-NetConnect cmdlet to determine if port 80 is open on the remote computer.

The Test-NetConnection cmdlet displayed diagnostic information for the specified connection. This cmdlet supports TCP tests, ping tests, route selection diagnostics, and route tracing. What this cmdlet returns? Based on the input parameters, we can have DNS lookup results, source/route address, computer name, and/or connection establishment confirmation in the output.

In the above example, we used -ComputerName and -Port parameters to specify the IP address or Domain Name System (DNS) name and port number. In the output, the value of the TcpTestSucceededdenotes whether the connection is established. If it is True, it means the connection is successfully established; otherwise, not.

We can also use the alias of the Test-NetConnection cmdlet as follows.

We can also run the command without writing the -ComputerName parameter name; see the following example.

Let’s make this command more user-friendly and inform them whether the given port is open via a customized message. For that, we use the if-else block as follows.

Here, we used the if statement to check the value of the TcpTestSucceeded; if it is True, the Write-Host cmdlet from the if block would be executed; otherwise, from the else block.

Use Test-NetConnection Cmdlet with -InformationLevel Parameter

Use the Test-NetConnection cmdlet with the -InformationLevel parameter set to Detailed to get more details about the established connection.

The value of -InformationLevel can also be set to Quiet to get a Boolean value about the established connection. The value will be True if the connection was successfully established; otherwise, False.

As demonstrated below, let’s add the if-else block in the above command.

Using System.Net.Sockets.TcpClient Class

Use the System.Net.Socket.TcpClient Class to check if the specified port is open.

First, we used the New-Object cmdlet to instantiate the System.Net.Sockets.TcpClient class, which means creating an object of this class which we stored in the $tcpClientObj variable. Then, we used the $tcpClientObj variable to access the Connect() method and passed the DNS name and port number to it.

After that, we accessed the Connected property of the $tcpClientObj within the’ if’ statement to check if its value is True. If so, then the Write-Host cmdlet from the if block was executed to display a message saying the connection is established. Otherwise, the else block will be executed.

We can use the above code by specifying the IP address to the Connect() method; see the following example.

That’s all PowerShell check if port is open.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *