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
(23)
38,537 times
Add to favorites
10/28/2016
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Looks promising but having trouble.
    2 Posts | Last post November 01, 2016
    • Direct from the download I try to execute to see if it works.
      The contents of my file are 
      
         . .\Connect-Mstsc.ps1
      
         Connect-Mstsc -ComputerName localhost -User mydomain\admin -Password abc123
      
      The output i get is 
      
      Security Warning
      Run only scripts that you trust. While scripts from the Internet can be useful, this script can potentially harm your
      computer. Do you want to run C:\aed\util\Connect-Mstsc.ps1?
      [D] Do not run  [R] Run once  [S] Suspend  [?] Help (default is "D"): r
      Missing statement after '=' in named argument.
      At C:\aed\util\Connect-Mstsc.ps1:119 char:42
      +     [cmdletbinding(SupportsShouldProcess= <<<< true,DefaultParametersetName='UserPassword')]
          + CategoryInfo          : ParserError: (:) [], ParseException
          + FullyQualifiedErrorId : MissingExpressionInNamedArgument
      
      Have you seen this before?   Am i using it wrong ?
    • Hello Daniel,
      
      My apologies for the tardy response, you can resolve this issue by running the following command:
      
      Unblock-File .\Connect-Mstsc.ps1
      
      
      This should remove the tag that the file was downloaded from the internet and allow you the run the function. 
      
      As for the error message about SupportsShouldProcess, this might be because you are running this on PowerShell 2.0. I wrote a specific version that is supported in PowerShell 2.0:
      https://gallery.technet.microsoft.com/scriptcenter/Connect-Mstsc-Open-RDP-c9060d94
      
      Please let me know if this resolves your issues.
      
      
      
      Kind regards,
      
      Jaap Brasser
  • .RDP File with this script
    5 Posts | Last post October 14, 2016
    • Hi Jaap,
      
      Thank you for this lovely script. May i know, will this script able to use with .rdp files? I've tried it but i cannot make it work. For example i've a set of .rdp file. I want to execute them using this script with username and password. 
      
      eg: Connect-Mstsc server01.rdp -User contoso\user1 -Password contoso
      
      Thank You
    • Hello ridhwn,
      
      What is your use case for this, do you have anything specifically configured in the RDP file, because otherwise you could just run the script like this:
      
      Connect-Mstsc server01.rdp -User contoso\user1 -Password contoso
      
      If you can let me know if you are looking to use any specific options that you have stored in the RDP file I could probably come up with a solution to make that work.
      
      
      Regards,
      
      Jaap Brasser
    • Hi Jaap
      
      Its a rdp file with certain redirection option . I couldnt make it run. It throw me error:
      My Command:
      Connect-Mstsc .\server.rdp -user contoso\user1 -Password contoso
      Error:
      Windows doesn't recognized the name specified in the computer name field. 
      
      If i run like this:
      Connect-Mstsc server.rdp -user contoso\user1 -Password contoso
      
      Error: 
      Remote Desktop can't find the computer server.rdp.........
      
      I think its parsed and connecting based on the file name. Not looking into the file for remote ip address. 
      
      Thank You
    • Hi Jaap
      
      Its a rdp file with certain redirection option . I couldnt make it run. It throw me error:
      My Command:
      Connect-Mstsc .\server.rdp -user contoso\user1 -Password contoso
      Error:
      Windows doesn't recognized the name specified in the computer name field. 
      
      If i run like this:
      Connect-Mstsc server.rdp -user contoso\user1 -Password contoso
      
      Error: 
      Remote Desktop can't find the computer server.rdp.........
      
      I think its parsed and connecting based on the file name. Not looking into the file for remote ip address. 
      
      Thank You
    • Hello ridhwn,
      
      That is correct, the script has no support for loading RDP files. The reason for this is because many of the options that are stored in the RPD file are not available as command parameters. So if you would like to open the .RDP file you could do the following:
      
      Invoke-Item .\server.rdp
      
      
      Let me know if that resolves your issue, otherwise I can take a look at alternative methods of doing this.
      
      
      Regards,
      
      Jaap Brasser
  • Command not loded in to session
    2 Posts | Last post October 14, 2016
    • PS C:\Users\myuser> .\Connect-Mstsc.ps1
      
      Security warning
      Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your
      computer. If you trust this script, use the Unblock-File cmdlet to allow the script to run without this warning
      message. Do you want to run C:\Users\myuser\Connect-Mstsc.ps1?
      [D] Do not run  [R] Run once  [S] Suspend  [?] Help (default is "D"): r
      
      PS C:\Users\myuser> Connect-Mstsc -ComputerName server01 -User contoso\jaapbrasser -Password supersecretpw
      Connect-Mstsc : The term 'Connect-Mstsc' is not recognized as the name of a cmdlet, function, script file, or operable
      program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
      At line:1 char:1
      + Connect-Mstsc -ComputerName server01 -User contoso\jaapbrasser -Passw ...
      + ~~~~~~~~~~~~~
          + CategoryInfo          : ObjectNotFound: (Connect-Mstsc:String) [], CommandNotFoundException
          + FullyQualifiedErrorId : CommandNotFoundException
      
      
      Suggestion [3,General]: The command Connect-Mstsc was not found, but does exist in the current location. Windows PowerSh
      ell does not load commands from the current location by default. If you trust this command, instead type: ".\Connect-Mst
      sc". See "get-help about_Command_Precedence" for more details.
      
      PS C:\Users\myuser>
      
      
      Any ideas?
    • Hello Lars,
      
      This is caused because I wrote this script as a function. This means that in order to be able to execute the function you first have to load this into your current session. This is done by 'dot sourcing' the script file, this loads the functions.
      
      Try the following:
      . .\Connect-Mstsc.ps1
      Connect-Mstsc -ComputerName server01 -User contoso\jaapbrasser -Password supersecretpw
      
      
      Kind regards,
      
      Jaap Brasser
  • nevermind.. other post today please remove.
    2 Posts | Last post August 26, 2016
    • I downloaded the version for powershell 2.0 and it appears to work.
    • No problem, glad to hear you found the solution. I have released PowerShell 2.0 versions of my most popular scripts for exactly this purpose. 
      
      Happy scripting!
      
      
      Regards,
      
      Jaap Brasser
  • Passwords With # Not Working with ConvertTo-SecureString
    2 Posts | Last post July 26, 2016
    • Hi Jaap,
      
      Thanks a lot for writing this script. It is super helpful.
      
      I am trying to establish a remote desktop connection with a password that has # in it. It works when I do this:
      
      Connect-Mstsc -ComputerName comp1 -User user1 -Password <Password with #>
      
      However, I do not prefer to pass the plain text passwork inline and hence tried
      
      Connect-Mstsc -ComputerName comp1 -User user1 -Password (ConvertTo-SecureString -String "Password with #" -AsPlainText -Force)
      
      This does not work with an error that the credentials did not work. I have tried putting the password in ' ' as well as '' ''. Neither works. 
      
      Is there any way I can make this work? Does it need settings or any changes on the remote host for this to work?
      
      Thanks in advance.
      
      Cheers,
      GC 
    • Hello GC1,
      
      The version that I have available on TechNet does not support securestrings as an argument for the password parameter. I decided to not implement that change in the TechNet version of this script. So I recommend that you either use the password parameter with a non-secure string or use the -Credential object if you want to include securestring as a default.
      
      You can create a credential object like this:
      New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('jaap',(ConvertTo-SecureString -String "Password with #" -AsPlainText -Force))
      
      Or in PowerShell 5.0 notation:
      [pscustomobject]::new('jaap',(ConvertTo-SecureString -String "Password with #" -AsPlainText -Force))
  • What to do about invalid credentials?
    2 Posts | Last post July 26, 2016
    • Hello, 
      
      I have multiple credentials because the company I work at has servers over many different domains. I am attempting to automate the RDP process and want to avoid any popups possible. When I attempt to do "Connect-Mstsc -ComputerName $ComputerName -User $Username -Password $Password"
      and the incorrect credentials are entered, it will bring up the RDP prompt asking for the correct credentials. Is there any way to have multiple credentials, or any way to avoid this pop up from occurring? 
    • There is no way of using multiple credentials, what you could do is run the command multiple times with different credentials each time. Because we are using the credentials in combination with a GUI tool this is the only option if you want to directly connect to the system.
      
      Alternatively, you could determine if an account has local administrative rights on a system by mounting the C$ share remotely using that set of credentials. I realize that this is not the same as having rights to RDP to the system but it is one method of testing the credentials. You could test this in combination with New-PSDrive and the -Credential parameter.
      
      
      Regards,
      
      Jaap Brasser
  • Can we verify that a RDP session is currently connected?
    2 Posts | Last post July 26, 2016
    • Hello Jaap,
       
      Is it possible to verify that a RDP connection has been made? If so, how would you check?
      
      Thank you,
      Will
    • Since starting a remote desktop session is a GUI interaction I would establish that a connection has been made by checking if I would be logged in to the remote system. 
      
      Alternatively you could check for the Window title, with a successful connection the title of the mstsc window should include the server name. So you could check with something along these lines:
      
      (Get-Process -Name mstsc) | Where {$_.MainWindowTitle -match "^$ComputerName"}
      
      This would give either true or false.
  • Connect-Mstsc powershell script asking for user credentials
    11 Posts | Last post May 02, 2016
    • Hello,
      
      I am using Connect-Mstsc.ps1 script to automatically start remote desktop session. However while executing this script it is able to launch mstsc and give us the prompt to provide user credentials though we have provided script parameters as computerName, User and Password.
      
      Can anyone suggest why mstsc showing prompt for user credential ?
    • Can you give me the exact line you are using to execute the script then I can have a look what is going wrong exactly.
    • I'm having the same issue.
      
      I'm attempting to use this script to log on a number of new user accounts for the first time so that their redirected folders are created on the server.
      
      $CSV='NewUsers1.csv'
      . (Join-Path $PSScriptRoot Connect-Mstsc.ps1)
      
      Import-Csv -Path $CSV | ForEach-Object { 
      	$username1 = "sjc\" + $_.UserAccount 
      	$password1 = $_.AccountPassword
      
      	write-host "un=" $username1 "pass =" $password1
      
      	Connect-MSTSC -ComputerName computername.domain -User $username1 -Password $password1 
          }
      
      When run the MSTSC credentials box is displayed on the screen and that's it.
      
      Using Windows 8.1 and Powershell 4.
    • Interesting, let's take a look if the credentials are stored correctly in the local credential store, could you execute the following and let us know the results:
      
      cmdkey.exe /list:TERMSRV/computername.domain
      
      Please replace computername.domain with the correct details of the system you are connecting to.
    • Thank you for your speedy response Jaap.
      
      The results of running that command are as follows;
      "Currently stored credentials for TERMSRV/computername.domain:
      
      * NONE *"
    • Appologise, I didn't put the correct computer name in.
      
      Currently stored credentials for TERMSRV/ict3pc03:
      
          Target: TERMSRV/ICT3PC03
          Type: Generic
          User: sjc\CIDA001
    • It is possible for systems to only accept manually entered credentials and to disallow logons with cached credentials. You can check if this is the case with ict3pc03 by doing the following:
      
      - Run: mstsc.exe
      - Enter the computername: ict3pc03
      - Verify if there is a checkbox for remembering credentials
    • Thank you so much for pointing me in the right direction and for creating this script. You've saved me a lot of time.
      
      I created user and computer group policies to allow the saving of passwords in remote desktop and applied them to my user account, my desktop and the server I'm connecting to.
      
      On first try I was still presented with a request for credentials. 
      
      I entered the details of the first user and clicked save password and then disconnected. 
      
      I then tried opening MSTSC and connecting to the same machine again and was once again presented with a credentials box. I clicked "show options" and noticed that there was a tick box labeled always request credentials. I unticked that and now the script is running as intended.
      
      # CSV file location for users ( two fields, UserAccount, AccountPassword )
      $CSV='NewUsers.csv'
      
      . (Join-Path $PSScriptRoot Connect-Mstsc.ps1)
      
      
      Import-Csv -Path $CSV | ForEach-Object { 
      	$username1 = "domain\" + $_.UserAccount 
      	write-host "un="$username1
      	Connect-MSTSC -ComputerName server.domain:3389 -User $username1 -Password $_.AccountPassword
      	Start-Sleep -s 30
      	Stop-Process -processname mstsc
          }
    • No problem at all and it seems like you only needed a nudge in the right direction to solve your issue. Setting up policies would have been my next suggestion but you have already done that.
      
      One thing I am curious about, what is the purpose of forcefully closing the connection after 30 seconds?
      
      
      Regards,
      
      Jaap Brasser
    • Thank you Jaap.
      
      There may be a more elegant way of doing it but I figured that 30 seconds should be long enough for the login process to have created the redirected folders on the file server and at that point I have no need for the session any more and didn't want to have 170 connections open at the end of the script.
      
      The accounts I'm creating are to be used in a computerised exam. 
      
      For each user I needed to create a folder on their desktop with their name, candidate number, etc as part of the folder name. 
      
      They can then save their work into that folder and come submission time we can just collect those all up and burn them on to disk for the exam board without worrying that the students have not named the folders as per exam board instructions.
      
      Now that I've done it this way I think a better approach would have been to just add the student's candidate number as an attribute in active directory for each of the user accounts and then just use a logon script which referenced this attribute to create the folder on first logon at the start of the exam. C'est la vie.
    • Thanks for sharing the background of your solution, I always find it interesting to see how others are using my scripts. 
      
      I agree that there are always different and more efficient ways of doing things, but if you have a working solution as-is I would not recommend on changing it yet. 
      
      If I may ask what kind of exams are you hosting?
      
      
      Regards,
      
      Jaap Brasser
  • can't get to work (win 8.1)
    2 Posts | Last post April 28, 2016
    • Hello.
      
      Thank you for creating this. 
      
      Unfortunately it does not work for me, when my script calls; 
      
      Connect-MSTSC -ComputerName $computername1 -User $username1 -Password $password1
      
      MSTSC launches but then request credentials. The credentials in $username1 and $password1 are correct and work when entered manually in the GUI.
    • I will answer your question in the other thread, where you posted your full code.
      
      
      Regards,
      
      Jaap Brasser
  • Script not initiating mstsc
    4 Posts | Last post April 28, 2016
    • When I execute the script, nothing happens.  Has anyone seen this behavior?  I am using PowerShell 4.0 on Windows 7 SP1 to control Windows 2008 R2
    • This script has a function embedded in the script, so if you would like to utilize this function you should dot source the script first, you can do this by typing the following line:
      
      . .\Connect-Mstsc.ps1
      
      Now the Connect-Mstsc function is loaded into your current sessions and you are able to use the function like you would any other cmdlet, for example by typing:
      
      Connect-Mstsc -ComputerName server01 -User contoso\jaapbrasser -Password supersecretpw 
    • That doesn't work Jaap.
      
      ..\Connect-Mstsc.ps1 : The term '..\Connect-Mstsc.ps1' is not recognized as the name of a cmdlet, function, script
      file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
      and try again.
      
      Both my script and the Connect-Mstsc.ps1 are in the same folder
    • You are missing the space in between both dots, so the syntax is wrong that is why the command fails for you.
      
      The first . is to indicate that you want to dot source the script file. The second .\ is to indicate the current folder. So try to run the following:
      
      . .\Connect-Mstsc.ps1
      
      Or specify the full path like this:
      
      . C:\Scripts\Connect-Mstsc.ps1
21 - 30 of 57 Items