Po$H Pete | Those who can… $cript
24Jan/118

Passing Username and Passwords to Remote Desktop (RDP) Automatically

In the past I've tended to use the rdp ocx controls and embed them into a web page. This allows you to pass in a username and password to get the RDP client to connect with. Unfortunately, the MSTSC.EXE program within Windows doesn't have any parameter options to pass in a username and password. therefore, you end up entering the command to connect, and then having to type in the username and password (*sigh*)

Fortunately, help is at hand, and it comes in the form of cmdkey.exe. This is built into Windows from Vista onwards, but if you're using XP, you can download it, have a quick google for it. Now, what cmdkey does is allow you to edit the stored credentials repository on your computer within the session that you are currently in. When MSTSC (RDP) connects to a remote computer it checks this repository to see if it has any matching credentials for the server it's talking to and then attempts to use them, if they are valid, you'll log straight into the desktop, excellent!

So, to wrap this all up neatly, here's a powershell function I wrote to do the connection for you. Just pass in a server name, user name and password and you're away...

Function Open-RDPSession($Server,$User,$Password)
{
 
	cmdkey /generic:TERMSRV/$server /user:$user /pass:$Password
	mstsc /v:$Server
 
 
}

Pretty simple, but it saves you having to remember the syntax!

Comments (8) Trackbacks (0)
  1. the script runs without any error , but doesn show any result.

  2. shiv,

    You need to create another PS1 file and call this function

  3. When you say “Just pass in a server name, user name and password” where exactly does that info go?

  4. Thanks for the idea!

    For people that were looking for this idea but want to use batch instead I created the following. Save this a mstscup.cmd from notepad (don’t forget to remove the .txt at the end!)

    Call it from the command line as follows:
    mstscup “servername” “user” “pass”

    I didn’t like the idea of leaving the user and pass in the password vault so it clears it after 120 seconds (leaves a command prompt window opened).

    Feel free to modify at will!

    @echo off
    setlocal

    :: Check if the user passed at least 1 argument
    if “%1%” == “” (
    echo Remoted Desktop connection with user and password
    echo.
    echo Missing arguments. Syntax:
    echo %~nx0% “servername” “username” “password”
    echo.
    echo Jean Morin, v0.1, 2013-02-23
    pause
    goto :eof
    )

    :: Next line removes surrounding quotes from server name
    set sServer=%~1%
    :: Keep the quotes for the username and password (in case spaces exists)
    set sUser=%2%
    set sPass=%3%
    :: Seconds to wait before clearing the newly added password from the vault (see control panel, manage your credentials)
    :: You may want to modify this if the server takes longer to connect (WAN). You could add this as a fourth argument.
    set sSeconds=120

    :: Add a new connection definition method to the vault
    cmdkey /generic:TERMSRV/%sServer% /user:%sUser% /pass:%sPass%

    :: Connect to the server as a new task
    start mstsc /v:%sServer%

    :: ping ourselves for x seconds (acts like a pause) then removes the newly added password from the vault
    ping -n %sSeconds% 127.0.0.1 >nul:
    cmdkey /delete:TERMSRV/%sServer%

  5. Thanks! it’s very useful

  6. If you dont want to use a function and then have to use a wrapper script to kick it off use this:
    $Server=”SERVERNAME”
    $User=”USERNAME”
    $Password=”PASSWORD”

    cmdkey /generic:TERMSRV/$Server /user:$User /pass:$Password
    mstsc /v:$Server

    I checked this on Windows 8 and 7 :)

  7. I’m trying to use this with Windows 8.1 but I allways get Credentials prompt. It worked on a Windows 7 or Windows 8 Machine.

  8. Works very fine directly:

    cmdkey /generic:TERMSRV/192.192.192.192 /user:valentin.badea /pass:myPassword
    mstsc /v:192.192.192.192


Leave a comment

No trackbacks yet.