Automatic Remote Desktop Connection

Note: This tip requires PowerShell 2.0 or above.

As a PowerShell user, you probably have a PowerShell console or the ISE editor on standby. Wouldn’t it be nice to be able to just hack in a “Connect-RDP” and immediately be connected to a remote desktop when you need it? And let PowerShell deal with login credentials? Here’s how.

Securely Caching Credentials

To securely cache login credentials, you can use the command line utility cmdkey.exe. With this utility, you can save a username and a password for a given remote connection. Windows will then securely cache the information and automatically use it when needed.

Connect-RDP – Auto-Login for RDP Sessions

Here is a function called Connect-RDP that automates the RDP connection:

function Connect-RDP {

  param (


  # take each computername and process it individually
  $ComputerName | ForEach-Object {

    # if the user has submitted a credential, store it
    # safely using cmdkey.exe for the given connection
    if ($PSBoundParameters.ContainsKey('Credential'))
      # extract username and password from credential
      $User = $Credential.UserName
      $Password = $Credential.GetNetworkCredential().Password

      # save information using cmdkey.exe
      cmdkey.exe /generic:$_ /user:$User /pass:$Password

    # initiate the RDP connection
    # connection will automatically use cached credentials
    # if there are no cached credentials, you will have to log on
    # manually, so on first use, make sure you use -Credential to submit
    # logon credential

    mstsc.exe /v $_ /f

Set or Update Cached Credentials

To cache credentials for a new remote desktop connection, this is how you’d call the function:

PS> Connect-RDP -Credential testdomain\Administrator

You would then be prompted for the connection password, and the RDP connection gets initiated. Internally, Connect-RDP stores the logon information in your credential cache. So from now on, to connect to the server via RDP, you no longer need the credentials. Next time, this is all you need:

PS> Connect-RDP

Using Multiple Connections

The function also supports multiple connections. If all of the connections require the same logon information, you can set it in one step:

PS> Connect-RDP,, -Credential testdomain\Administrator

If the connections require different logon credentials, then set the credentials individually:

PS> Connect-RDP -Credential testdomain\Administrator
PS> Connect-RDP -Credential testdomain\Testaccount12
PS> Connect-RDP -Credential testdomain\Tobias

Once you have set cached credentials for all your RDP servers, you can connect to one or many with just one call:

PS> Connect-RDP,,

PowerShell will use the appropriate cached credentials for each of these connections, and opens an RDP session for each server.

Manage Cached Credentials

To manage your cached credentials, use cmdkey.exe:

PS> cmdkey

Creates, displays, and deletes stored user names and passwords. The syntax of this command is:

CMDKEY [{/add | /generic}:targetname {/smartcard | /user:username {/pass{:password}}} | /delete{:targetname | /ras} | /list{:targetname}]


  To list available credentials:
     cmdkey /list
     cmdkey /list:targetname

  To create domain credentials:
     cmdkey /add:targetname /user:username /pass:password
     cmdkey /add:targetname /user:username /pass
     cmdkey /add:targetname /user:username
     cmdkey /add:targetname /smartcard

  To create generic credentials:
     The /add switch may be replaced by /generic to create generic credentials

  To delete existing credentials:
     cmdkey /delete:targetname

  To delete RAS credentials:
     cmdkey /delete /ras

PS> cmdkey /list:

Currently stored credentials for

  Type: Generic
  User: citrixdev\Administrator
Filed in: Articles, Columns, Online Only Tags: , ,

18 Responses to "Automatic Remote Desktop Connection"

  1. slestak says:

    Appreciate the tip. I left the credential parm out of mine. Did not want any caching of credentials.

  2. siddharth agarwal says:

    Hi, I am new to Powershell so here i have couple of doubts. Could you plz let me know that if i use the above PS script then where do i have to define the login credentials for multiple servers???

  3. Tobias Weltner says:

    Look at the examples please: if you have multiple servers that all require different login credentials, then call the function for each connection, and submit the appropriate logon credential. Once you have done that, you can use the function without -Credential for one or multiple servers. And slestak, if you do not want to cache logon credentials, then you can omit the -Credential parameter in the first place. However, then you could call mstsc directly, too.

  4. Peter Klumm says:

    line 23 brings me the error: CMDKEY: This command requires either the /u switch and a username or else the /smartcard switch with a smartcard
    Doing the command manually works fine..
    Any clue?

  5. Tobias Weltner says:

    Oh, blanks in passwords – did not think about that 🙂 Please quote the parameter in the code:
    cmdkey.exe “/generic:$_” “/user:$User” “/pass:$Password”

    Does that fix it for you?

  6. Hi, I new with user in PS but keep try so bginer in job. you have any *pdf about this case? #tks

  7. jason dumlao says:

    Hi Tobias! Good morning to you. I’m just a beginner so I might ask a dumb question. I have question regarding your script. I was testing it out the other day and your script works fine if the server your login to belongs to the same domain as the user account your using, but when you login to a server that belongs to another domain what ends up happening is it will still cache your credentials and will still go thru the rdp process but it will stop at the login screen and it will ask you again for your username and password. I tested this on a windows 2003 and windows 2008 server multiple times. Any thoughts on this problem?

  8. david dumlao says:

    Hi Tobias! Good Morning to you. I would like to ask for your help, I tried your powershell script and I found an issue in which the server/s that your log into is on a different domain it will rdp into the server/s but it will stop at the login prompt and ask you again for your username and password even if put on the username this “domain\username” but that issue is doesn’t come up if the username your using belongs to the same domain as the servers your log into. Can help me modify your script. Sorry I’m still new in powershell. Thanks

  9. Sri says:

    Hi Tobias! Great Solution .
    I need your help to enhance the script which will exceute powershell on the remote server ( exists on the remote server) and get the output file to local server .
    Daily I am logging into the server and capturing info in text file.Recently I have implemented 1 script in remote server which is getting CPU usage , drive total space and free space and giving the output in html . I don’t have admin rights on the remote machine . So I want to copy the output file to my local machine. So that I can mail this to my team DL. Hope you understand my issue.

  10. Why do you use the /generic switch? Is there a difference when using generic credentials and domain credentials? (https://msdn.microsoft.com/en-us/library/windows/desktop/aa380517%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396) Maybe I just don’t understand the difference.

  11. Upendra Patel says:

    Hello Tobias

    I have a couple of questions.
    I copied and pasted the Connect-RDP code into ultrEdit and saved it. what should be the file extension for my file.

    When I tried to run the routine with a parameter, I got the following.
    Connect-RDP : The term ‘Connect-RDP’ 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-RDP
    + ~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Connect-RDP:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    last ?
    does this routine work behind the scenes, or I expect to see the fields typed in.
    or is a connection to the remote computer made automatically

    Thank you for your support and time

  12. Keerthichandra says:

    My username contain backslash (\) then, how can I differentiate username and password. Please help me here.

  13. Dilip says:

    I supplied user name and password, and doing something like this.

    cmdkey /add:$Server /user:$User /pass:$password
    mstsc /v $server

    This still asks me for the credentials… Any thoughts?

    • herbert says:

      For me it worked to change
      cmdkey /add:$Server /user:$User /pass:$password
      cmdkey /generic:$Server /user:$User /pass:$password

  14. Irfan says:

    We are able to establish the RDP but ending with a landing page of gateway server with security message and need to click ok.
    How can we by-pass this security message and click ok thing.

    Can we parameterized this?

Leave a Reply

Submit Comment

© 2018 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: