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
48,209 times
Add to favorites
E-mail Twitter Digg Facebook
  • Try Catch with multipile computers
    2 Posts | Last post November 17, 2019
    • Hello, not sure if this is possible since the script ends with kicking off mstsc but I"m trying to log computers that don't successfully connect. I was thinking of some kind of try catch implementation but cannot get that to work thus far.
    • Log for not connected/rejected machines would be extremely helpful.
  • Public Operator
    1 Posts | Last post May 23, 2019
    • Hello,
      your script is very usefull for me but when I try the public parameter mstsc asked me for credentials and don't use them provided in the command. If I don't use the public parameter the connection will be established but then windows saves the credential informations. I've used windows 10 for my tests with your script.
      Any ideas for getting the public parameter working for me and use the provided credentials in the command?
      Thanks for your help.
      Greetings Manuel
  • mask password
    1 Posts | Last post January 18, 2019
    • Hi Jaap,
      as usual, a nice script. 
      However I'd like the password that I'm entering to be masked instead of appearing in clear text on my screen.
      Maybe build on the Get-Credential command or use some PSObject?
  • Audit Log
    1 Posts | Last post December 11, 2018
    • Hello,
      I have managed to incorporate your script in an automated function logging in and starting applications on dummy (automation) accounts.
      And it is working as a charm.
      But, as I try to use it in automated manner, an error handling would be useful. 
      I have tried to use the standard .redirectStandardOutput  property, but is is returning NULL 
                  $ProcessInfo.FileName    = "$($env:SystemRoot)\system32\mstsc.exe"
                  $ProcessInfo.Arguments   = ".\rld.rdp", "$MstscArguments /v $Computer"
                  $ProcessInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Normal
                  $processinfo.RedirectStandardOutput = $true
                  $Processinfo.RedirectStandardError = $True
                  $processinfo.UseShellExecute = $false
                  $Process.StartInfo       = $ProcessInfo
                  if ($PSCmdlet.ShouldProcess($Computer,'Connecting mstsc')) {
                  commandTitle = $commandTitle
                  stdout = $process.StandardOutput.ReadToEnd()
                  stderr = $process.StandardError.ReadToEnd()
                  ExitCode = $process.ExitCode
      And result is:
      commandTitle stdout stderr ExitCode
      ------------ ------ ------ --------
      In case of questions I use the rdl.rdp file to automatically start my application at logon and nothing else.
      Do you know any method to redirect the errors from mstsc.exe to this function output (like access denied, incorrect password etc.)? 
  • Adding custom parameters to Remote Desktop
    1 Posts | Last post June 14, 2018
    • If I wanted to add a custom parameter to remote desktop like I would in a standard .rdp file, is it possible to do this in the script?
      For example, I usually include smart sizing in my RDP sessions by adding "smart sizing:i:1" to the RDP file. Is there a way to turn on Smart Sizing via this script?
  • Remote Desktop Gateway
    1 Posts | Last post April 14, 2018
    • Hello Jaap,
      This is a very useful script, thanks for making it.
      As we are using Remote Desktop Gateway, I added the following parameter:
                  [string]       $Gateway
      and MstscArgument:
                  {$Gateway}    {$MstscArguments += "/g:$Gateway "}
      Please feel free to include it in your script as well.
      Best Regards,
  • Copy & Paste of Files not working
    1 Posts | Last post March 23, 2018
    • Hello Guys, 
      i working a lot with different VMs so i use the following stuff in my Powershell Profile:
          function cred()
          $global:PSC = Get-Credential
          function rdp()
          param ([string]$a)
              if (!($PSC))
          Connect-Mstsc -ComputerName $a -Credential $PSC -Width 1440 -Height 900
      As you can see it starts an RDP Session with specific settings. My Problem is that i cannot copy&paste files from or to this session. Only Text Copy/Paste is working. When i open an rdp session from my windows it is possibile to use copy/paste. Does anyone has a similar problem?
      Best regards, Michele
  • Connect to server using IPv6
    1 Posts | Last post February 15, 2018
    • Is it possible to connect with IPv6 address (not FQDN or alike).
  • How do I use ConvertTo-SecureString?
    3 Posts | Last post January 26, 2018
    • Hi Jaap,
      Sorry if this is a bit of an obvious question, I'm pretty new to PS.
      I'm trying to make a script has the credentials saved to open a remote session without any user input.
      I'm running this:
      Connect-Mstsc -ComputerName XXXX -User XXX\XXXXX -Password (ConvertTo-SecureString "XXXXXXXXXXXXXXXX" -AsPlainText -Force)
      When I run that script, it says the password is incorrect and a login box prompts me to enter the password, which then allows me to login. If I run the script without using ConvertTo-SecureString then the script works ok, but I don't want to expose the password to the computer I'm logging into.
      Is there something I am doing wrong?
      If possible, I am looking to add a timeout after the user has remoted in. For example, the user would disconnect automatically after 10 seconds. Have you got any suggestions for the best way to accomplish this?
    • hey jbrowne91, you make and progress with this? I'm looking to accomplish the same task here^ and running into the same issue. Would greatly appreciate any recommendations/guidance if you've made some progress.
    • This is probably a bit late, but for anyone else with the same problem, the answer is to not use the -name and -password options, use the -credential option instead.  Create a $cred variable ($cred = get-credential 'domain(or computername if it's not in a domain)\user') and use that as the value for -credential (-credential $cred).  You'll be prompted for the password in a popup and the encrypted information will be stored in the $cred variable.
  • Need to publish the result
    1 Posts | Last post January 03, 2018
    • Hi Jaap,
      Thanks for the script..
      But can you please help me with the below scenario
      We have a daily task to check RDP is working fine for some 100 servers and it's logging into the server with out any issue.
      Can you please come with some dashboard kind of (html format) output to display which all are the servers not in network, profile loading issue, Connected succesfully.
      Also need to logoff from those 100+ server once completed
      Appreciate your help on this..
1 - 10 of 57 Items