Tech Blog‎ > ‎

PowerShell - Check If Remote Port Is Open

posted Jan 7, 2010, 8:07 AM by Victor Zakharov   [ updated Jan 7, 2010, 8:09 AM ]
There are tons of ways to check if the port is open on a remote server. I think I found the fastest one, which is a Powershell one-liner:

(new-object Net.Sockets.TcpClient).Connect($host, $port)

For testing purposes you can use the following command:

(new-object Net.Sockets.TcpClient).Connect("google.com", 80)

It should give you no results. This means connection succeeded.

In real life you may encounter the following two outputs in red:

(1) Exception calling "Connect" with "2" argument(s): "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond xxx.xxx.xxx.xxx:xx"

or

(2) Exception calling "Connect" with "2" argument(s): "No connection could be made because the target machine actively refused it xxx.xxx.xxx.xxx:xx"

It depends on the endpoint firewall configuration. For the server, which receives too many connection requests, it is more natural to drop connections (1) rather than refuse them (2).
Comments