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


  • My requirement: rdp to a server, no userid or password.
    3 Posts | Last post April 21, 2016
    • I need to mstsc to servers for existence only. I will not have a userid and password. Before I download this function, will this work and return a different return code if the server is not up?
      
      Also, I don't want the console to appear or I need to funnel it to somewhere. I tried normal Powershell standard output redirects but the screen keeps popping on my server that I issue mstsc from.
      
      I used a SSH function you wrote by the way. I think it was yours. I had the same issue with the SSH screen popping. I need to ditch the console.
      
      If you have thoughts hit me back. Thanks for the code you put out!!!
    • The easiest way to do this is to check the server port, if you are using PowerShell 4.0 or later you could do this:
      
      Test-NetConnection -ComputerName yourserver.jaapbrasser.com -Port 3389 -InformationLevel Quiet
      Test-NetConnection -ComputerName yourserver.jaapbrasser.com -Commonport RDP -InformationLevel Quiet
      
      If you are using older versions of PowerShell then the System.Net.Sockets.TcpClient .Net object would be the way to go.
    • Jaap, I keep looping back to this check that I'm stuck on. I setup Test-NetConnection but is also uses sockets in the check. I lose my socket thru my network path, so it appears to be connected when the far end server is down. I tried routing RedirectStandardOutput to a variable or file, mstsc doesn't put anything in there for the local process. It launches fine, I see it and I want to pipe to some file or somewhere. 
      
      You code here is/was a big help. I used it for another situation. 
      If anyone hears of how to pipe mstsc (terminal server port I think) to something let me know.
  • Nothing happens
    2 Posts | Last post April 12, 2016
    • I have tried what you suggested to another user, but still nothing happens.
      I'm using Windows 7 and PS v2
    • Hello FrankTobias,
      
      Can you please elaborate to what you are trying to do, what you are expecting, the syntax you are using and if there are any errors, please copy paste the full errors here.
      
      
      Regards,
      
      Jaap Brasser
  • No window pop up
    2 Posts | Last post April 07, 2016
    • Hello,
      I am trying to run your script Via a batch file that allows a user to input the computer they want to connect to. I am not a programming person at all. This is what i have gotten so far:
      
      @echo off
      set /p Computer= What Computer would you like to connect to?
      Powershell.exe -ExecutionPolicy Bypass -file "C:\Users\user\Desktop\Connect-Mstsc.ps1"
      powershell.exe .\Connect-Mstsc -ComputerName %Computer% -user %Computer%\User -Password pw
      Pause
      
      I am running into problems with getting past the security (Which i have been able to just nuke) and it won't open a window to the computer for me to interact with.
      
      Running on windows 10 as well. 
      
      
    • Hello BenBauk,
      
      If user interactivity is what you want, perhaps you could use the Show-Command cmdlet for this purpose, for example by executing PowerShell like this:
      
      powershell.exe -command "& {. C:\Scripts\Connect-Mstsc.ps1;iex $(Show-Command Connect-Mstsc -PassThru)}"
      
      Does this satisfy your requirements?
  • Error when running
    4 Posts | Last post March 30, 2016
    • Hello,
      when i enter the command . .\Connect-Mstsc.ps1 it returns:
      The "=" operator is missing after a named argument.
      At c:\..*\connect-Mstsc.ps1:119 char:42
      [cmdletbinding<supportsshouldProcess, <<<< DefaultParametersetName='UserPassword'>]
      CategoryInfo: ParserError: <:> [], ParseException
      FullyQualifiedErrorId: MissingEqualsInNamedArgument
      
      I'm running windows 7 64bit
      
      Maybe im just to stupid to find the mistake, or im during it wrong, Anyways hope that you can help me :) 
    • Hello Turtle932,
      
      This is probably because you are running PowerShell 2.0, I released a version of Connect-Mstsc that is PowerShell 2.0 compatible. It is available here:
      https://gallery.technet.microsoft.com/scriptcenter/Connect-Mstsc-Open-RDP-c9060d94
      
      Let me know if you also experience the problem if you use that version of the script.
      
      
      Regards,
      
      Jaap Brasser
    • Hello Jaap,
      
      It is working! Thanks alot! :) 
    • No problem, glad to hear the other version is compatible with your version of PowerShell. Happy scripting Turtle!
  • Special characters in passwords.
    4 Posts | Last post January 27, 2016
    • The script does not work when the user password contains a double quote character. I spent a few minutes on the command line trying to get cmdkey to accept a password with a single quote character without success. Does anyone have a solution?
      
      PS C:\powershell> 'rd.ad.contoso.com' | Connect-Mstsc -User contoso\jennifer -Password '1A2Z"I7x' -Width 1024 -Height 768
    • This is an interesting problem, I have taken a look at it but it appears that cmdkey does indeed not like quotation marks in the password. It will not complete the command as it requires user input. I have a few workarounds in mind for this issue, but I haven't finalized the new code yet.
      
      I will give an update here as soon as I have finalized the new code, thanks for reporting this bug I was unaware that this was a limitation of cmdkey.
    • Any progress on getting cmdkey to work with passwords containing quotation marks and other special characters?
    • Thanks for the reminder, I will get back to you this week on this!
  • Why won't this run?
    4 Posts | Last post January 14, 2016
    • I really apologize if this is a stupid question, but I can't get this script to run.  I'm invoking it from a powershell prompt just as outlined in the examples (Connect-Mstsc -ComputerName <server>).
      
      Doing it through GUI works, doing it through mstsc.exe (with normal authentication) works, I have set the execution policy to unrestricted, I can't get any code to execute within the 'process' block (tried simple 'Write-Host')...
      
      I'm running PowerShell 4.0 on a Windows 7 SP1 machine.  Am I missing something?  Anything helps!  Thanks!
    • No problem, it is a common problem as I tend to write my scripts as functions to make them more easily accessible in your PowerShell session. So to be able to use this function you should first 'dot source' the script, after dot sourcing the script the function(1) in a .ps1 file become available for the duration of your PowerShell session.
      
      So execute the following:
      . .\Connect-Mstsc.ps1
      Connect-Mstsc -ComputerName yourserver -User you -Password Donottellme
      
      This should work, let me know how that works for you.
    • Ugh... now I see it.  Sorry for my ignorance, thank you so much for your help!
    • No problem Paul, happy to help!
  • How do I wait for the RDP session to finish?
    4 Posts | Last post January 12, 2016
    • Currently, I can't find a way to wait for the rdp session to finish (is there one?).
      
      If I invoke Connect-Mstsc from my powershell script, it immediately returns and the next statement will be executed.
      It would be very useful to add a switch "-Wait" so that my script can react to the end of the rdp session.
      An example with Start-Process and mstsc: 
      Start-Process "$env:windir\system32\mstsc.exe" -ArgumentList "/v:$machinename" -Wait 
    • I found a solution and suggest it as an extension of your script.
      
      1. Add the new parameter "Wait":
              [Alias("WT")]
                  [switch]$Wait
      2. Wait for the process mstsc, if "Wait" is true:
                  if ($PSCmdlet.ShouldProcess($Computer,'Connecting mstsc')) {
                      [void]$Process.Start()
                      if ($Wait) {
                          [void]$Process.WaitForExit()
                      }                  
                  }
       
    • Thanks Rosch,
      
      I appreciate the suggestion and I will implement it in the script and give you credits for it. Thanks for helping out to make the script more useful, the power of the community at work!
    • Just wanted to let you know I have updated the script, thanks for providing the code sample I have tested and implemented it.
  • remotedesktop gateway
    2 Posts | Last post January 12, 2016
    • dear jaap,
      
      do you have any command line parameters for remotedesktop gateway services?
      
      regards josef
    • Can you give me an example of what settings you would like to include in the script?
  • Durga
    2 Posts | Last post November 25, 2015
    • when i try this script objecting remote system it seems in order that message i got"the server's authentication policy does not allow connection requests using saved credentials"
    • If a specific host is configured either by using local policy or by a group policy then you should either change the policy on that system or you will not be able to this function to automatically connect to the remote system.
      
      The policy that appears to be in place is there to block exactly what you are attempting to do with this function, so there is no workaround for this.
  • Great Script - You could hide the password
    3 Posts | Last post November 24, 2015
    • Hi,
      
      Great script. Just a note though, when you do not specify the password as a param, and you are prompted, the input isnt masked. You can mask the password input by using
      [Security.Securestring]$Password instead of [string]$Password
      
    • i also had to amend line 156 to $Password = $Credential.Password to accept the new type when used with $credentials
    • From a security point-of-view I understand your concern, and I do agree that using Securestring is a more secure option than using a regular string. It will however also make the script harder to use, as this:
      
      Connect-Mstsc -ComputerName server01:3389 -User contoso\jaapbrasser -Password supersecretpw
      
      Will change into:
      
      Connect-Mstsc -ComputerName server01:3389 -User contoso\jaapbrasser -Password (ConvertTo-SecureString -String 'supersecretpw' -AsPlainText -Force)
      
      So my recommendation would be to use the -Credential object in combination with Get-Credential, if security is a concern. 
31 - 40 of 57 Items