9

How do you specify a user when using the mstsc command in powershell? While I can specify the server, I cannot specify a user.

Let's assume Server name = server01, User name = Test, Password = PW

Example 1 mstsc /v:server01 /user server01\test /password PW

This only brings up the "Remote Desktop Connection Usage" help menu.

Example 2 mstsc /v:server01

This works, bringing up the normal RDP connection prompt for User & password.

Example 3 mstsc /v:server01 /user server01\test

Even trying to just specify the user fails, bringing up the help menu again.

Some website articles on powershell suggest using "Connect-RDP" or "Connect-Mstsc" instead of just "mstsc" as per above examples, but this only brings up an error code. My knowledge of powershell is very basic, so I'm probably making a simple mistake somewhere.

5
  • Using this script gallery.technet.microsoft.com/scriptcenter/… and running the command Connect-Mstsc -ComputerName 18.186.48.107:3389 -User workpc\Yisroel -Password YisroelGithub -Verbose Jul 19, 2018 at 3:23
  • Yisroel, this still brings up an error Connect-Mstsc is not recognised
    – SteveM
    Jul 19, 2018 at 3:53
  • It is not a built-in command. You need to download the Connect-Mstsc.ps1 script file from the link I mentioned. Then cd to the script file location (cd C:\Users\Yisroel\Downloads) then run . .\Connect-Mstsc.ps1 and then that script with the details Jul 19, 2018 at 4:10
  • Yisroel, still not working sorry. Downloaded script, and ran as above. Error: " ...cannot be loaded because running scripts is disabled". Ran as admin: "Set-ExecutionPolicy -executionpolicy undefined". Still getting error
    – SteveM
    Jul 19, 2018 at 4:46
  • 1
    To run a script from anywhere you need to first run Set-ExecutionPolicy Unrestricted Jul 19, 2018 at 4:55

3 Answers 3

8

From the "Remote Desktop Connection Usage" help menu, there is no switch like "/user" or "/password".

Please try

cmdkey /generic:"server01" /user:"test" /pass:"PW"

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

2
  • @Yisroel Tech and V_V, both methods worked once I unticked "Always ask for credentials" in Remote Desktop Connection settings.
    – SteveM
    Jul 19, 2018 at 22:57
  • 3
    Preferably you should use cmdkey /add:TERMSRV/server01 /user:Test /pass:PW And if you want the credentials to be deleted after mstsc has launched you can do this by invoking the command cmdkey /delete:TERMSRV/server01
    – gollum
    Sep 10, 2018 at 13:36
2

I want to note here that if your domain administrators have disabled caching of credentials none of the suggested answers will work.

3
  • exactly what i'm having happen. so this is useless then Mar 18, 2021 at 15:16
  • Yes. About your only option then is to download a free macro recorder and let that do the login for you. Of course then your password is stored plaintext on your machine. Mar 19, 2021 at 16:30
  • no there is another way - i've seen a product called Eggplant do it. I just don't know how it does it. Mar 20, 2021 at 15:40
1

It has been said before, but I'd just like to add a fully working, batch-ready example that I use when I need to log in to so several user's accounts on a simple (i.e. the same password for all users) terminal server, e.g. when I need to make certain changes to each user's environment.

For %f in (user1 user2 user3) do cmdkey /add:TERMSRV/servername /user:domain\%f /pass:commonpassword & mstsc /v:servername & ping 127.0.0.1 & cmdkey /delete:TERMSRV/servername

(replace "servername", "domain", "commonpassword" and the usernames in the for-loop as needed. You may need to open MSTSC once without parameters to set the default parameters as needed and then save the settings.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .