22

I want to use mstsc /admin to login to a server silently. My batch file reads the code as

mstsc /v:xxx.xxx.xxx.xxx /admin

But it ask me to enter password. Can anyone help me to skip this step?

I went on to Google and found this site. But I am very new to this stuff (scripting) and could not understand what to do with given code. Is it a vbscript file? Can I do the same thing with a batch file. Please elaborate and I request you to spoon feed. This is not my arena but still I am pushed to fight without weapons.

My basic need is to kick off all users from a remote desktop except mine to perform some maintainable work. Thanks.

Sandy
  • 11,332
  • 27
  • 76
  • 122

7 Answers7

56

Re-posted as an answer: Found an alternative (Tested in Win8):

cmdkey /generic:"<server>" /user:"<user>" /pass:"<pass>"

Run that and if you run:

mstsc /v:<server>

You should not get an authentication prompt.

Angelo
  • 1,407
  • 1
  • 13
  • 18
  • 2
    It works, but the password becomes saved in your system. – Andre Soares Apr 25 '16 at 20:35
  • 6
    @AndreSoares true, but you can delete it with cmdkey /delete:servername or ip. This procedure works in windows 7. – Evilripper Apr 11 '17 at 10:57
  • It helped me to create a solution for Windows 10. Thanks. See [my answer](https://stackoverflow.com/a/46617521/6186647). – Milad Oct 07 '17 at 06:51
  • PowerShell cleanup of all saved credentials if you have a lot `$remove = cmdkey /list | & { Process { If($_ -like "*Target=*"){$_.Split("=")[1].Trim()} } }; $remove | & { Process { cmdkey /delete:$_ } };` – Bitcoin Murderous Maniac Feb 11 '21 at 22:32
9

the command posted by Milad and Sandy did not work for me with mstsc. i had to add TERMSRV to the /generic switch. i found this information here: https://gist.github.com/jdforsythe/48a022ee22c8ec912b7e

cmdkey /generic:TERMSRV/<server> /user:<username> /pass:<password>

i could then use mstsc /v:<server> without getting prompted for the login.

Dili
  • 556
  • 1
  • 5
  • 14
8

to be secured, you should execute 3 commands :

cmdkey /generic:"server-address" /user:"username" /pass:"password"
mstsc /v:server-address
cmdkey /delete:server-address

first command to save the credential

second command to open remote desktop

and the third command to delete the credential for security reason

all of these commands can be saved in a batch file(bat). (if you save these command in a batch file, third command will not be executed until you close the remote desk)

Ali Ziaee
  • 569
  • 5
  • 10
  • There's a gotcha here though, if you delete it too quick with the third command before the first two finish, the connection with fail. It's best to wait and run the deletion after the connection is established already. – Bitcoin Murderous Maniac Feb 11 '21 at 22:34
  • in response to the first comment, if you save these command in a batch file, third command will not be executed until you close the remote desk. – Ali Ziaee Mar 27 '21 at 07:16
  • Does not work if the target server does not allow logging in with saved credentials – Pui Ho Lam Feb 27 '23 at 10:11
7

Same problem but @Angelo answer didn't work for me, because I'm using same server with different credentials. I used the approach below and tested it on Windows 10.

cmdkey /add:server01 /user:<username> /pass:<password>

Then used mstsc /v:server01 to connect to the server.

The point is to use names instead of ip addresses to avoid conflict between credentials. If you don't have a DNS server locally accessible try c:\windows\system32\drivers\etc\hosts file.

Milad
  • 539
  • 8
  • 21
  • PowerShell cleanup of all saved credentials if you have a lot `$remove = cmdkey /list | & { Process { If($_ -like "*Target=*"){$_.Split("=")[1].Trim()} } }; $remove | & { Process { cmdkey /delete:$_ } };` – Bitcoin Murderous Maniac Feb 11 '21 at 22:32
1

It became a popular question and I got a notification. I am sorry, I forgot to answer before which I should have done. I solved it long back.

net use \\10.100.110.120\C$ MyPassword /user:domain\username /persistent:Yes

Run it in a batch file and you should get what you are looking for.

Sandy
  • 11,332
  • 27
  • 76
  • 122
  • Does not appear to work. Still prompts for username. – Angelo Dec 04 '14 at 00:19
  • Did you replace the "domain\username", "MyPassword" and IP Address in above command with your values? Also make sure you have credentials of Admin. – Sandy Dec 04 '14 at 07:03
  • 1
    Found an alternative (Tested in Win8): cmdkey /generic:"" /user:"" /pass:"" – Angelo Dec 05 '14 at 03:33
0

Save your username, password and sever name in an RDP file and run the RDP file from your script

Steven Ackley
  • 593
  • 7
  • 31
0

The Command mentioned above didn't work for me (Windows 11)

cmdkey /generic:"server-address" /user:"username" /pass:"password"

When i listed the cmdkeys using the commad:

cmdkey /list

I saw, that a legacy target was added. Adding a specific TERMSRV-Target worked for me:

cmdkey /add:Domain:target=TERMSRV/<SERVER> /user:<DOMAIN>\<USER> /pass:<PASSWORD>
Daniel
  • 486
  • 4
  • 19