Powershell script to test RDP is available

rated by 0 users
Not Answered This post has 0 verified answers | 9 Replies | 4 Followers

Top 500 Contributor
11 Posts
Patrick posted on 09-12-2012 12:02 PM

I am looking to write a script to test a server to see if the RDP port is available. Is there a way to do so?

All Replies

Top 100 Contributor
39 Posts

First you want to make sure the computer is Pingable. Here is a "Pingable" snipit (https://snipt.net/dvsit/fastpingable/). Once you know it is pingable then test the port.

But check the registry if you can: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server value fDenyTSConnection



Top 10 Contributor
2,170 Posts
Microsoft MVP
Top Contributor


can you explain why try to ping at first, please?

Ping is a command sent on ICMP protocol which is by default disabled on most corporate networks because of secutiry reasons (at least at incoming nodes, ping amplification attacks), and AFAIK blocked by firewall on Windows 7 by default. The RDP service is listening on TCP 3389 by default. Of course trying to connect to the service and recieving logon error or connection is the best test but it should be ok to test if the port is listening.

the process of testing a port over network is documented here:


For local (or remote powershell) the simplest way is probably WMI documented in link to the Technet, you have posted.


Not Ranked
3 Posts

Yo dude, there certainly is a way to do this, and the script is already written for you.

It pings, it checks the RDP port (3389) and any other ports you want to check, and it even beeps at you when it is successful.

Just follow this link:


Top 10 Contributor
1,832 Posts
Top Contributor

Another one I like is this one by Justin Dearing, Autoconnect RDP:



This one first pings, then automatically open an mstsc session once RDP is available.

Not Ranked
3 Posts

Nice! My wish-list for this one would be to add the capability to pass security credentials so you don't have to key in domain\user & password. I don't think that is supported and no longer allowed to be passed in clear-text using an .rdp connection file (which makes sense).

What I'm thinking would be cool is if there was a capability to pass an encrypted credentials file (e.g. http://poshtips.com/2011/03/04/import-and-export-user-credentials-in-powershell/) to speed up the logon process while somewhat maintaining security. Again, I don't think this would be supported by MSTSC but it might be possible via an API.

Top 10 Contributor
1,832 Posts
Top Contributor

Yea, when you said it I thought that might be useful. I looked for some methods to be able to facilitate this and wrote a short function that provides this functionality.

I posted it on Technet, have a look at it there:

Connect-Mstsc - Open RDP Session with credentials


Let me know what you think of it.

Not Ranked
3 Posts

Very cool and nice work-around there using cmdkey! I Like It!

My next thought is that I still need to securely store user credentials so I can feed them into your function to be used by cmdkey. I suppose that this could be done using the script I referenced earlier. e.g. pull the secured credentials from an encoded xml file, feed that into the connect-mstsc function and be on our merry way.

Any thoughts?

Top 500 Contributor
20 Posts

Hi Team,

Myself new to power-shell scripting and  started working on few basic scripts.

Could anyone help me with testing RDP session or remote connection available for multiple computers. This is basically needed to check after bulk reboot post patching activity. 


Top 10 Contributor
1,832 Posts
Top Contributor

Hello Maheshroja,

Could you please start a new thread as your issue does not directly relate to this thread. We like to keep threads separated so it is easier to search for solutions for people with similar issues or questions.

Page 1 of 1 (10 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.