Checking an open port in Powershell - one liner
I have found and used many Powershell scripts to check open ports. Most of these are overkill for what I am trying to do. I typically want to test that the port I opened in a firewall answers. There are many great scripts you can run to test this, but if all I want to do is test if the port is open this is the quickest way:
(new-object Net.Sockets.TcpClient).Connect("microsoft.com",80)
If the port is open, it will just return you to the command prompt:
PS C:\Windows\system32> (new-object Net.Sockets.TcpClient).Connect("microsoft.com",80)
PS C:\Windows\system32>
If it fails, you will see an error after a timeout delay:
PS C:\Windows\system32> (new-object Net.Sockets.TcpClient).Connect("microsoft.com",1433)
Exception calling "Connect" with "2" argument(s): "A connection attempt failed because the connected party did not prop
erly respond after a period of time, or established connection failed because connected host has failed to respond 65.5
5.58.201:1433"
At line:1 char:43
+ (new-object Net.Sockets.TcpClient).Connect <<<< ("microsoft.com",1433)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
PS C:\Windows\system32>
Comments