RDP a server by command line

Jim2010 Morrison2010 1 Reputation point


Can someone please help me to share the command line that I can RDP servers using my user name and account.

Connect-Mstsc -ComputerName '<VM Name>' -User '<domain/username>' -Password '<password>'

When I tried the above command I am getting this error:

Connect-Mstsc : The term 'Connect-Mstsc' is not recognized as the name of a cmdlet, function, script file, or operable

I tried this too, but no luck:
mstsc /v:<VM Name> -User '<domain/username>' -Password '<password>'

I am getting the RDP windows for those servers if I just use the below commands. But RDP window asking my password (User name is automatically taking):
Connect-Mstsc -ComputerName '<VM Name>'


mstsc /v:<VM Name>

So my need is by a command line (user name and password), I need to connect the server via RDP properly.

Can someone please help ?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,344 questions
{count} votes

5 answers

Sort by: Most helpful
  1. Andreas Baumgarten 95,026 Reputation points MVP

    It's not possible to add the username and password to the mstsc.exe.

    But this should work:

    # Create credentials
    cmdkey /generic:"<servername>" /user:"<username>" /pass:"<password>"
    # Connect MSTSC with servername and credentials created before
    mstsc /v:<servername>
    # Delete the credentials after MSTSC session is done
    cmdkey /delete:TERMSRV/<servername>

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Andreas Baumgarten

    2 people found this answer helpful.

  2. Andreas Baumgarten 95,026 Reputation points MVP

    Could you please try the cmdkey without the quotes:

    cmdkey /generic:<servername> /user:<username> /pass:<password>

    If this doesn't work the next you can try:

    cmdkey /TERMSRV:<servername> /user:<username> /pass:<password>

    If you have 100+ servers you have to connect why you need to connect via command line? I just want to understand the use case.

    There are very good Remote Management Solutions on the market to get manage and connect via RDP to servers with integrated credential management. I am using Royal TS on a daily base: https://www.royalapps.com/ts/win/features

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Andreas Baumgarten

    1 person found this answer helpful.

  3. Ricardo Bohner 1 Reputation point

    Why don't you create an *.rdp file you can add your username and password there. Than you run it via command line or however you want.

  4. Andreas Baumgarten 95,026 Reputation points MVP

    Saving the RDP config in a *.rdp file with MSTSC:

    Enter Servername and click on Show options


    Enter username and check the Allow me to save credentials
    Save the RDP file with Save as
    Connect to the server


    Here you will find a full how-to: https://www.nextofwindows.com/how-to-save-password-in-a-remote-desktop-connection-in-windows-8


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Andreas Baumgarten

    0 comments No comments

  5. DaveK 1,846 Reputation points

    Hi, I would recommend mRemoteNG as a connection manager. It allows you to import servers directly from AD or from a list in a text file and if a lot of the servers use the same credentials, it allows you to specify them at top level and choose per server if you inherit those creds or you can select certain servers for different creds as needed. Very useful if you manage lots of servers

    0 comments No comments