I believe testing the ports after a Firewall maintenance or in another network related scenario could be real tiresome. Well, PowerShell enables us to automate this stuff as well.

Now no more running the cmd prompt and firing the telnet queries from your local or a remote machine to some destination computers:) (2)


We will use two classes of System.Net.Sockets namespace, to query the ports like the way we test them using the Telnet utility.

  1. System.Net.Sockets.TCPClient : Class to create an object to query  the status of TCP ports
  2. System.Net.Sockets.UDPClient : Class to create an object to query  status the of UDP ports

To understand these classes better, you can refer the Microsoft article Here , which describes the properties and methods that could be utilized as per our requirement by the instances of these classes.

Tesing TCP ports : I would say TCP part is pretty easy and reliable, which is the reason of the popularity of TCP (Transmission Control Protocol) which establishes a connection and sends an ACK (Acknowledgement). which makes it reliable and straightforward to use, like below


Testing UDP ports : UDP ports are tricky to test and results aren’t reliable as UDP protocol doesn’t send ACK’s (Connectionless protocol) so testing them is difficult. As there is no guarantee that you’ll receive a response.

To workaround this we’ll use System.Net.Sockets.UDPClient‘s  Send() method to send Datagrams to destination IPAdress:Port and expect (Wait) for a response from the destination.


NOTE :  Please note in most of the cases you won’t receive a response, as UDP protocol is a connectionless protocol, but when you’ll run the UDP port test in a loop, you will see some responses getting captured from the destination confirming that port is open. Like in below animation.



Feed in Source, Destination and Port Number to the function and run it to get the status of Ports.

The script prompt for your Administrator Credentials if you give source other than you local machine, So that it can log onto remote Source and  invoke Telnet-Port function to test destination ports.


Also, Protocol  is set to  ‘TCP‘  By default, like in the animation below. (1)

If you want  UDP  Protocol use the switch -Protocol followed by UDP to test the UDP ports, like below –

Moreover, there is an -Iterate switch through which you can Continuous Telnet, just like we do continuous ping’s (ping -t


This function made my life a bit easy in some network related testing, Hope helps you in the same way.

Hope you’ll find it useful and Thank you so much for stopping by.