PowerShell :: Automatic Remote Desktop Connection

Using PowerShell function “Connect-RDP” we can rdp servers using secured cached credentials, it can be used to RDP single/multiple servers using cached credentials

To cache credentials on PowerShell command line we need to cmdkey.exe and the target server name for which you want to cache the credentials or single cached credential can be used against multiple servers.

Save the script in .ps1 powershell extension file type
Open PowerShell command line
To call the PowerShell function in the current PowerShell session you need to execute “. .\Connect-RDP.ps1”
To list the cached credentials on PowerShell command line type –> cmdkey /list
To cache credentials against a single server(target) we can use –> cmdkey/add:targetname /user:username /password:password

Example:– For target testpc1 in below screenshot uses domainname\user username to connect the server.
Same way we can use multiple server names to connect using cached credentials.. for example :- Connect-RDP server1 server2 server4








The another way to cache credential against a target is to run the Connect-RDP function with switch -Credential with the username to connect




To RDP server run command –> Connect-RDP TargetServer – PowerShell credential request window will appear



Select drop down button under User Name section and select the cached user name using which you want to connect the server

credential list







Once you select the user name click on OK button
The server will be connected(RDP) using the securely cached user name

logon to windows



Connect-RDP.PS1 script content

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


About asifkhandevadi

Hello, I have been working on windows since 9 years and currently working as windows, VMware and MS clustering SME at IBM. Whenever I get free time I participate in Microsoft forums and write some blogs to enhance my technical and communication skills through knowledge sharing. Please contact me on FB or Linkedin if you need any assistance on troubleshooting, implementation and virtualizaton.
This entry was posted in Power-Shell, Windows and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s