Run mstsc.exe with specified username and password

Process rdcProcess = new Process();
rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe");
rdcProcess.StartInfo.Arguments = "/generic:TERMSRV/192.168.0.217 /user:" + "username" +  " /pass:" + "password";
rdcProcess.Start();

rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\mstsc.exe");
rdcProcess.StartInfo.Arguments = "/v " + "192.168.0.217"; // ip or name of computer to connect
rdcProcess.Start();

The above code initiates a connection with .217 and I am not being prompted to provide a password. Thanks for help.


If you want to use powershell you could add the credentials using

cmdkey /generic:DOMAIN/"computername or IP" /user:"username" /pass:"password"

Then call RDP connection using

Start-Process -FilePath "$env:windir\system32\mstsc.exe" -ArgumentList "/v:computer name/IP" -Wait

If you want to delete the credentials run

cmdkey /delete:DOMAIN/"Computer name or IP"

Remember to remove ""

Tags:

C#

Rdp