Connect-Mstsc - Open RDP Session with credentials (PowerShell 2.0)

This script allows you to open a Remote Desktop session with a remote session while specifying credentials. This functionality was removed from the mstsc.exe application and this PowerShell function can serve as a workaround that allows you to automatically connect to servers.

5 Star
4,418 times
Add to favorites
E-mail Twitter Digg Facebook
  • I would like keep last 7 files on each folder
    1 Posts | Last post May 03, 2018
    • Hi Japp,
      I would appreciate if you provide me the syntax to keep last 7 files from each folder. Basically, the goal is recycling my folders with last 7 files (any no of days old).
      Thank you in advance.
  • How to avoid disclaimer message prompted when we take RDP?. Is there any parameters or way to pass to skip the disclaimer message?
    1 Posts | Last post October 27, 2017
    • $Server='target_server'
      cmdkey /generic:TERMSRV/$Server /user:$User /pass:$Password
      mstsc /v:$Server
      I am using the above command to take rdp but we are landing into disclaimer message which is asking to accept the policies and after we click ok it allows to login to the RDP server machine.
  • Clearing the stored credential
    1 Posts | Last post September 27, 2017
    • Since cmdkey stores the credential, to prevent security and password change issues it might be a good idea to follow up this script with this command to delete the stored credential:
      cmdkey /delete TERMSRV/HOSTNAME
      (replace HOSTNAME with the name of the host)
  • Its asking for credentials &computername
    3 Posts | Last post September 15, 2017
    • Its asking for computer name. I cannot proceed after that. how do i skip everything & directly log in?
    • i am using a batch file to run the PS script
    • Can you share what you are running exactly so I can verify what is going wrong?
  • Don't understand how to use the script
    4 Posts | Last post August 29, 2016
    • Hello,
      I tried to launch the script in different ways, but I can't get it to work.
      Anytime I try, I get no result.
      Here are my different tries :
      C:\Script>powershell -version 2.0 -File Connect-Mstsc.ps1 -ComputerName -User Administrator -Password password0
      C:\Script>powershell -version 2.0 -File Connect-Mstsc.ps1 -ComputerName -User Administrator -Password password04
      C:\Script>powershell  -File Connect-Mstsc.ps1 -ComputerName -User Administrator -Password password04
      C:\Script>powershell  -File Connect-Mstsc.ps1 -ComputerName -User Administrator -Password password04 /f
      PS C:\Script> .\Connect-Mstsc.ps1 -ComputerName -User Administrator -Password password04
      PS C:\Script> .\Connect-Mstsc.ps1 -ComputerName -User Administrator -Password password0
      PS C:\Script> . .\Connect-Mstsc.ps1 -ComputerName -User Administrator -Password password0
      PS C:\Script> . .\Connect-Mstsc.ps1
      PS C:\Script> .\Connect-Mstsc.ps1
      What am I doing wrong ? Thank you in advance.
    • Hello Raoul,
      This problem here is that you first need to load the function in memory, this is done by dot sourcing it. Try out the following two examples, one is for executing this script from outside of PowerShell the second example assumes you are in a PowerShell console:
      powershell -noprofile -nologo -command "& {. C:\Git\SharedScripts\Connect-Mstsc\Connect-Mstsc.ps1;Connect-Mstsc -ComputerName -User Administrator -Password password04
      From PowerShell:
      . .\Connect-Mstsc
      Connect-Mstsc -ComputerName -User Administrator -Password password04
      Jaap Brasser
    • Thank you Jaap, I tried the "ouside of PowerShell" solution, it works just fine !
      Thanks again !
    • Here to help Raoul, glad to hear it is working for you.
      Jaap Brasser
  • operator missing?
    2 Posts | Last post August 29, 2016
    • Jaap,
      I am attempting to run this by ". .\connect-mstsc.ps1" and am met by an error:
      The "=" operator is missing after a named argument.
      At C:\Users\jbond\Connect-Mstsc.ps1:119 char:42
      +     [cmdletbinding(SupportsShouldProcess, <<<< DefaultParametersetName='UserPassword')]
          + CategoryInfo          : ParserError: (:) [], ParseException
          + FullyQualifiedErrorId : MissingEqualsInNamedArgument
      Do you have any suggestions for me?
    • Yes, Jason I presume you are running the PowerShell 3.0 and higher version. This PowerShell 2.0 version should not generate this error. Can you verify which version you downloaded and provide me with the version number, you can view this in the .ps1.
      Jaap Brasser
  • cant seem to get this to run from ps command
    2 Posts | Last post August 29, 2016
    • tried Connect-Mstsc -ComputerName server01  with my own servernames of course.
      would running on VPN prohibit or running windows 10?
    • That really depends on your network configuration. Is it possible for you to connect to the system by typing mstsc /v:yourservername, if that works then this script will also work.
      Jaap Brasser
  • Status Returns?
    7 Posts | Last post April 25, 2016
    • Jaap,
           I keep thinking I can use this set of code. If you don't get a good connection, meaning RDP is not available or server down, do you return a status code I can leverage off of. I'm on MS R2012 now. I have to get this thing done. I don't have to supply a userid or password, just get up/down for the protocol. I can't use Sockets, so I have to try to log in.
    • Hello Rocky,
      I have been seeing this question more often recently, using the Microsoft tools available there are a number of things you can do:
      - Verify if the port is open 3389,unless manually configured differently
      - Verify if the RDP service is running on the system
      - Remotely query for any open session
      - Connect by using mstsc.exe, a GUI approach
      Since mstsc.exe is a GUI application we can use this to verify if it is possible to open a connection but it does not return any status codes.
      At the moment there is no method of doing what you are requesting but I will look into this to see if there are any valid alternatives available.
    • I'm thinking names pipe on the port that mstsc uses. I can then pipe that into something but I have no idea if this is possible even on LINUX. I've done named pipes on UNIX a commands, not on MS platform.
    • So is there any reason in particular that you can't use something like Test-NetConnection on port 3389 to verify if RDP is functional?
    • As for monitoring, the closest I have found online is this Nagios check. It goes slightly beyond just testing the port, I have not tried this code myself but it seems to be on the right track:
      Jaap Brasser
    • I will check on this Nagios check. The issue with Test-NetConnection is the socket level that it runs on. We have a situation where IPforwarding and some other bouncing around occurs, so the socket can't be trusted. We have tested this out using Test-NetConnection  and the MS R2008 socket capabilities in PowerShell, the socket stays open and looks alive, but the far end is down. A real bummer. I really dissected Test-NetConnection, it doesn't really say is uses sockets but the options off the command show that it does. They things are what they are and we try to use them the best we can. Your help here has been wonderful!!
    • No problem Rocky,
      And regardless if you fix the problem or if you hit a dead end, be sure to check back here and we can see how we can either improve your solution or find another alternative.
      Happy to help Rocky and good luck!
      Jaap Brasser
  • Prompts for Credentials
    2 Posts | Last post January 14, 2016
    • Every time I run this, even supplying credentials, the MSTSC app asks for my credentials.   My goal is to automate this happening overnight.   
      What do I have set wrong?
    • Can you show me how you run the command, which parameters are you using. Also which operating system are you running the script on?