One line in powershell to check if remote port is open

Here is an easy way to use powershell to see if a remote machine is listening on a specific port. If the command finishes without returning anything the remote machine is listening on the port. If the commands times out or return an error the machine does not listen on that port or your request was blocked by an firewall or ACL rule.

Code:

#Check TCP connection
(New-Object Net.Sockets.TcpClient).Connect("<remote machine>",<port>)

#Check UDP conection
(New-Object Net.Sockets.UdpClient).Connect("<remote machine>",<port>)
This entry was posted in Scripting and tagged , , , , , , , , , , , . Bookmark the permalink.

6 Responses to One line in powershell to check if remote port is open

  1. Vidhya says:

    Thanks so much! Such a life saver for quick troubleshooting!

  2. nick says:

    You are the literal best you have no idea how hard it was to find a way to do this on win7 powershell

  3. Martin Naula says:

    Thank you for sharing your knowledge with us, works well

  4. Tony says:

    Thanks,
    Nice to have in my PS-Tools

  5. Eye Choonz says:

    Not working for me.
    I am trying to check whether UDP port 53 is open on a DC.
    PS C:\WINDOWS\system32> (New-Object Net.Sockets.UdpClient).Connect(“DC01”,53)
    …does nothing and sure i know its open, but:
    PS C:\WINDOWS\system32> (New-Object Net.Sockets.UdpClient).Connect(“DC01”,) also does nothing.
    Any chance you could explain how to use this ?

Leave a Reply

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