Table of Contents
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
withlocalhost
. 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 thenetstat -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.
1 2 3 |
Test-NetConnection -ComputerName example.com -Port 80 |
1 2 3 4 5 6 7 8 |
ComputerName : example.com RemoteAddress : 93.184.216.34 RemotePort : 80 InterfaceAlias : Wi-Fi SourceAddress : 192.168.xx.xx TcpTestSucceeded : True |
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 TcpTestSucceeded
denotes 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.
1 2 3 |
tnc -ComputerName example.com -Port 80 |
1 2 3 4 5 6 7 8 |
ComputerName : example.com RemoteAddress : 93.184.216.34 RemotePort : 80 InterfaceAlias : Wi-Fi SourceAddress : 192.168.xx.xx TcpTestSucceeded : True |
We can also run the command without writing the -ComputerName
parameter name; see the following example.
1 2 3 |
tnc example.com -Port 80 |
1 2 3 4 5 6 7 8 |
ComputerName : example.com RemoteAddress : 93.184.216.34 RemotePort : 80 InterfaceAlias : Wi-Fi SourceAddress : 192.168.xx.xx TcpTestSucceeded : True |
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.
1 2 3 4 5 6 7 8 |
$connection = Test-NetConnection -ComputerName example.com -Port 80 if($connection.TcpTestSucceeded){ Write-Host "Connection Established." }else{ Write-Host "Connection isn't Established." } |
1 2 3 |
Connection Established. |
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.
1 2 3 |
Test-NetConnection example.com -Port 80 -InformationLevel Detailed |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
ComputerName : example.com RemoteAddress : 93.184.216.34 RemotePort : 80 NameResolutionResults : 93.184.216.34 MatchingIPsecRules : NetworkIsolationContext : Internet IsAdmin : False InterfaceAlias : Wi-Fi SourceAddress : 192.168.xx.xx NetRoute (NextHop) : 192.168.1.1 TcpTestSucceeded : True |
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
.
1 2 3 |
Test-NetConnection example.com -Port 80 -InformationLevel Quiet |
1 2 3 |
True |
As demonstrated below, let’s add the if-else
block in the above command.
1 2 3 4 5 6 7 8 |
$connection = Test-NetConnection example.com -Port 80 -InformationLevel Quiet if($connection){ Write-Host "Connection Established." }else{ Write-Host "Connection isn't Established." } |
1 2 3 |
Connection Established. |
Further reading:
Using System.Net.Sockets.TcpClient
Class
Use the System.Net.Socket.TcpClient
Class to check if the specified port is open.
1 2 3 4 5 6 7 8 9 |
$tcpClientObj = New-Object System.Net.Sockets.TcpClient $tcpClientObj.Connect('example.com', 80) if($tcpClientObj.Connected){ Write-Host "Connection Established." }else{ Write-Host "Connection isn't Established." } |
1 2 3 |
Connection Established. |
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.
1 2 3 4 5 6 7 8 9 |
$tcpClientObj = New-Object System.Net.Sockets.TcpClient $tcpClientObj.Connect('93.184.216.34', 80) if($tcpClientObj.Connected){ Write-Host "Connection Established." }else{ Write-Host "Connection isn't Established." } |
1 2 3 |
Connection Established. |
That’s all PowerShell check if port is open.