Connect-Mstsc - Open RDP Session with credentials

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.

4.7 Star
38,460 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Nothing happens...
    3 Posts | Last post July 28, 2015
    • Hi, thanks for the script, but nothing happens when I execute the command:
      .\Connect-Mstsc -ComputerName myserver -User domain\user -Password My Password
      Also, does the script support spaces in passwords?
    • No problem the script should be able to handle a password with spaces just fine, just put quotation marks around the password. Could you give the following code a try:
      . .\Connect-Mstsc;Connect-Mstsc -ComputerName myserver -User domain\user -Password My Password
      I have included the . .\Connect-Mstsc.ps1 portion of the code, to load the Connect-Mstsc function in memory. This only needs to happen once per PowerShell session.
    • Sorry I mistyped the code example, please use the following example:
      . .\Connect-Mstsc.ps1;Connect-Mstsc -ComputerName myserver -User domain\user -Password 'My Password'
  • Feature Request: /public
    3 Posts | Last post July 02, 2015
    • Can you add a `Public` parameter, mapped to the `/public` switch?
      I would add a default value of $true.
    • Hello MSFTW,
      Yes I will add this in the next update of this script, thanks for the suggestion. I will let you know once I have updated my script.
    • Hello MSFTW, I have added in the -Public parameter. Let me know if it works as intended for you.
  • Connect-mstsc with a backslash in the Password
    2 Posts | Last post May 28, 2015
    • Hi Japp,
      Your script fit the bill perfectly.
      I'm having an issue though with a password that has a '\' in it.  Tried passing single quotes, to no avail.
      Any suggestions?
      Using this in a dos .cmd file:
      PowerShell.exe -Command "& {. d:\io-stp-d\ps_scripts\Connect-Mstsc_P2.ps1; Connect-Mstsc -ComputerName %sServer% -User %username% -Password %PASSWORD%}" >>%logfile%
    • Hey Matt,
      I have tried to reproduce the error but for me the script works fine with backslashes in passwords, can you confirm that you are experiencing the same behavior if you are not using the command prompt variable but hard coded credentials instead?
  • Gateway Server
    2 Posts | Last post May 06, 2015
    • Thanks for the script. Can I add a gateway server as well?
    • I cannot add it to the script as it cannot be specified as a command line parameter for the mstsc command. You could specify the gateway server and options for the gateway server by editing Default.rdp file in your my documents folder. In fact I might write a script for that if there is not one available. I will let you know if I create a script with that functionality.
  • Error when when dot sourcing script
    2 Posts | Last post April 09, 2015
    • Hi Jaap,
      I am running the script exactly as copied from the download link on a windows 7 workstation. I get the following error:
      The "=" operator is missing after a named argument.
      At H:\connect-mstsc.ps1:102 char:42
      +     [cmdletbinding(SupportsShouldProcess, <<<< DefaultParam
          + CategoryInfo          : ParserError: (:) [], ParseExcep
          + FullyQualifiedErrorId : MissingEqualsInNamedArgument
      Line 102 it refers to is:
      I am really looking forward to using the script. it will really solve a problem we have here.
      Any assistance would be appreciated. 
      Many Thanks,
    • Great question, in my enthusiasm I uploaded the Powershell 3.0 version without updating my post properly. I am now maintaining a separate version that is PowerShell 2.0 compatible but does support the new features such as the -Credential parameter. I have linked it in the description, the PowerShell 2.0 version is available here:
  • issue with rdping to a different domain
    7 Posts | Last post April 08, 2015
    • Hi Jaap! Good afternoon. I was trying to use your script to automatically login to different computers from different domain using a single account. So lets say I do Connect-Mstsc, mis\jaapbrasser -password supersecretpw this command will let you login automatically and it will not ask you for a username and password, the issue now is when when you do this command Connect-Mstsc, mis\jaapbrasser -password supersecretpw after you enter that command it will still load rdp but it stops at the windows login screen where it ask you for a username and password. But when you do it normally like open rdp and enter the server name, username and password it lets you login with out any issues. Can you modify your script to include that function. Thanks
    • Just to reiterate the problem you are running into, the problem is that when you are connecting from one domain to a system in another domain the script is not working. But when you connect from to a system in the same domain the script works as expected?
      I am asking this so I can try to reproduce the error in order to be able pinpoint the issue.
    • When you log into a different domain the script would save your credentials on the cmdkey, would start up mstsc but it will stop at windows logon screen in which its asking you to enter choose your domain, type your username and password. But if you log in to a server that in same domain, the script will log you in automatically with your credentials.  Its like the script doesn't switch to the domain your username belongs. I think that's the problem. 
    • Jaap, I think whats happening is when you put on the command -user mis\jaapbrasser the script doesn't tell the mstsc that the username is on another domain or doesn't detect it that your telling mstsc that the account is on another domain. I there a way to add another parameter and use the log on to dropdown box  on the mstsc?
    • Just to make sure I know exactly what you are doing, you are using your MIS-domain credentials to login to a server in the dmz-domain? Is that correct, then I can try to reproduce the situation to have a look at how I can resolve this in an updated version of the script.
      Can you give me a short description of the following factors:
      - Code you are using to execute the script
      - What domains and the relationship between domains (same forest or not, what kind of trust between the domains)
      - In which domain is the user account located
      Let me know if you can share that information, it would be nice if we could tackle this as I was not aware that the script did not work in certain cross-domain environments.
    • I'm doing this on the companies network not remotely or the dmz. I'm using your code and I enter the command similar to this Connect-Mstsc, mis\jaapbrasser -password supersecretpw  the domains have a two way trust. 
    • Hello Engr.djdumlao,
      I have made some improvements to the scripts, can you verify if you are still running into this issue when using the updated, 1.2, version of the script?
  • mstsc still asking for credential
    2 Posts | Last post March 18, 2015
    • as Vipul_Dafada mstsc is still prompt me to provide username and password when using the script. 
      What is working for me is 
      Start-Process mstsc -ArgumentList "/v servername" -Credential $cred   
    • Thanks that is a nice alternative solution, thank you for sharing that. So what is the exact code you are using for which the script does not work as expected?
  • Exclamation point in password
    2 Posts | Last post March 18, 2015
    • Hey there Jaap! Great script. I use a password that begins with an "!" exclamation point. Unfortunately the script fails with an error "cannot be recognized as a valid Prompt command." as soon as I type in my password.
      Is there any way to make sure this works with special characters in the password? I've tried a variety of different ways but I've had no luck. Thanks for your time and consideration.
    • Can you show me the exact code you are using, then I can have a look at while it fails. You could try to wrap the password in single quotes ' ' and see if that resolves the behavior.
  • Adding a program launch string?
    2 Posts | Last post March 18, 2015
    • Jaap -
      I'm really new to PowerShell, so forgive the ignorant question and/or any terminology issues please. I'm wondering if this script could be repurposed to include an additional action. I'm thinking of using a script like this to remote into a machine and then start up a program. The end goal would be to add the modified script to a scheduled task and have it send out an email to a distro list if it was able to connect and start up a program. Is it possible to modify this script to include some of that, or am I better off starting over? Any tips for creating one or modifying this one?
      Many thanks!
    • Starting an application remotely is something you can already do, with Invoke-Command. If it is a GUI application which you may or may not want to interact with then I would recommend using this script to initialize the remote desktop connection and use an UI Automation tool such as the UI Automation PowerShell Extensions for the automation once you are connected to the remote system.
  • This script is totally awesome !!
    2 Posts | Last post March 18, 2015
    • When executing automated tests with Visual Studio you must have a Remote desktop open so you must use this script.
      This is a crucial oneliner for everybody who wants to call this script within a batch script:
      PowerShell -Command "& {. .\Connect-Mstsc.ps1; Connect-Mstsc <server> <user> <password> -Admin;}"
    • Thanks for sharing that one-liner, that is indeed a correct method of launching this script from a scheduled task/command prompt or Visual Studio. I will update the integrated help of the script to include this one liner as this is a helpful one-liner.
41 - 50 of 57 Items