PowerShell : Telnet TCP\UDP ports on Multiple Machines


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:)

ezgif.com-video-to-gif (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.

ezgif.com-gif-maker (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.



4 thoughts on “PowerShell : Telnet TCP\UDP ports on Multiple Machines

  1. It looks very neat, but I cannot get it to work.

    Which version of PowerShell is required?

    I’m trying to run it from a Windows Server 2016 machine but not even getting an error message when running it?
    Also tried on Windows 10 Creators Fall Edition 1709

    I’ve saved the script to a file “Test-PortConnectivity.ps1” and then trying to run it with command below.

    PS D:\PS> .\Test-PortConnectivity.ps1 TestPC 3389 -Protocol TCP -Iterate

    PS D:\PS>

    As you can see no output not even an error?! 🙂




    1. Small update, I read the text again and actually followed the instructions… see I forgot the single-quotes for IP and Hostname in the example…
      So I should have typed this instead as the example DID specify. 🙂

      Test-PortConnectivity ‘’ ‘TestPC’ 3389 -Protocol TCP -Iterate

      Although, still it works when using ‘’ but if I specify a real IP-address then I get this error.

      [xxx.xxx.xxx.xxx] Connecting to remote server xxx.xxx.xxx.xxx failed with the following error message : The WinRM client cannot process the request. Default auth
      entication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explic
      it credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more inf
      ormation on how to set TrustedHosts run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.
      + CategoryInfo : OpenError: (xxx.xxx.xxx.xxx:String) [], PSRemotingTransportException
      + FullyQualifiedErrorId : CannotUseIPAddress,PSSessionStateBroken

      Again, reading in your text.

      “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.”

      But, should I really need provide credentials to log on to the remote server to be able to see if I can get a connection to it???
      Sounds a bit weird, could you please explain further why this is required?

      Have a nice day.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s