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

About the author: Tobias Weltner

Tobias is a long-term Microsoft’s MVP Award recipient located in Germany. He has written more than 130 IT books published by Microsoft Press and others and just released his “PowerShell 5 Windows Automation” hardcover and e-book with O’Reillys. In April 2016, he initiated the world’s largest PowerShell Conference “” in Hannover, Germany. He focuses on developing the ISESteroids PowerShell editor extension, delivering PowerShell inhouse trainings throughout Europe, and PowerShell-related project work with mid- and large-size enterprises, and is one of Europe’s leading PowerShell trainers and experts. For training or project work inquiries, public or inhouse, contact him at You can follow him on twitter@TobiasPSP.

Related Posts