Skip to content

Instantly share code, notes, and snippets.

@jdforsythe
Last active October 23, 2024 11:54
Show Gist options
  • Save jdforsythe/48a022ee22c8ec912b7e to your computer and use it in GitHub Desktop.
Save jdforsythe/48a022ee22c8ec912b7e to your computer and use it in GitHub Desktop.
Remote Desktop Auto Login Powershell Script
cmdkey /list | ForEach-Object{if($_ -like "*target=TERMSRV/*"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}
echo "Connecting to 192.168.1.100"
$Server="192.168.1.100"
$User="Administrator"
$Password="AdminPassword"
cmdkey /generic:TERMSRV/$Server /user:$User /pass:$Password
mstsc /v:$Server
# https://technet.microsoft.com/library/hh847748.aspx
# Run PowerShell as Administrator
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nolog -command cmdkey /generic:TERMSRC/some_unc_path /user:username /pass:pa$$word; mstsc /v:some_unc_path
cmdkey /list | ForEach-Object{if($_ -like "*target=TERMSRV/*"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}
echo "Connecting to somethingspecial.cloudapp.net"
$Server="somethingspecial.cloudapp.net"
$Port="49982"
$User="Administrator"
$Password="AdminPassword"
cmdkey /add:$Server /user:$User /pass:$Password
mstsc /v:$Server:$Port
@deviaSteve
Copy link

Thanks for this. For other using this instead of the MS powershell script. You can use a credential object to pass in the password if you've collected that like I had in a script when running against an Azure build script.
$password = $cred.GetNetworkCredential().Password

For example:

RDP

echo "Connecting to " + $vm.Name
$Server=$nic.IpConfigurations[0].PrivateIpAddress
$username = $cred.UserName
$rdppassword = $cred.GetNetworkCredential().Password
cmdkey /generic:TERMSRV/$Server /user:$username /pass:$rdppassword
mstsc /v:$Server

echo "You MUST be on the VPN to connect to the private IP"

@saurabh683
Copy link

connect.ps1, is failing to load the password to mstsc, hence not bein ale to login, can someone please suggest anything?

@aldabrada
Copy link

aldabrada commented Apr 30, 2018

@saurabh683 I get the same thing. The RDP window pops out but with no connection to the server.

I'm getting "the command line parameters are incorrect" CMDKEY

@indayush
Copy link

indayush commented Nov 6, 2019

connect.sp1 works
Thanks a lot. Although to automate it, i would suggest the users to put this code in a ps1 fle & launch it via a seperate bat file.

@jcconnell
Copy link

I believe the one-line-cli.bat should have a single dash in front of the command portion:

c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nolog -command cmdkey /generic:TERMSRC/some_unc_path /user:username /pass:pa$$word; mstsc /v:some_unc_path

@jdforsythe
Copy link
Author

I believe the one-line-cli.bat should have a single dash in front of the command portion:

c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nolog -command cmdkey /generic:TERMSRC/some_unc_path /user:username /pass:pa$$word; mstsc /v:some_unc_path

Fixed

@duginivijay
Copy link

Can anyone advice how to automate the process of clicking yes on pop up of certificate errors?

@prince11jose
Copy link

How to disconnect a RDS session without the installation of RDS Role and use "Disconnect-RDUser"

@lavie3k
Copy link

lavie3k commented Oct 15, 2020

Hi. How to check login PASS or FAIL via command line ?

@gyanu89
Copy link

gyanu89 commented Feb 3, 2021

I had created script using above defined method it was working fine almost 6 months then it's started showing an issue -
Problem / Issue - "CMDKEY: Not enough memory resources are available to process this command." and RDP not picking user and password as defined in script.
Script -
$user = $null
$server = $null
$serverPassword = $null
$server = Read-Host "Enter Server Name"
$user = "AdminID"
$serverPassword = "AdminPassword"
cmdkey /generic:TERMSRV/$server /user:$user /pass:$serverPassword
mstsc /v:$server /w:800 /h:600

@gyanu89
Copy link

gyanu89 commented Feb 3, 2021

I had created script using above defined method it was working fine almost 6 months then it's started showing an issue -
Problem / Issue - "CMDKEY: Not enough memory resources are available to process this command." and RDP not picking user and password as defined in script.
Script -
$user = $null
$server = $null
$serverPassword = $null
$server = Read-Host "Enter Server Name"
$user = "AdminID"
$serverPassword = "AdminPassword"
cmdkey /generic:TERMSRV/$server /user:$user /pass:$serverPassword
mstsc /v:$server /w:800 /h:600

--
Above issue has been fixed by modifying script code is available : https://github.com/gyanu89/AutomatedRDP_Script.git

@sundeep4sandy
Copy link

connect.ps1, is failing to load the password to mstsc, hence not bein ale to login, can someone please suggest anything?

Hello,

I'm actually facing the same issue. i used the code as above. The script is launching an RDP but unable to enter the password in it. Can you let me know how you fixed this issue?

@gyanu89
Copy link

gyanu89 commented Aug 5, 2021

connect.ps1, is failing to load the password to mstsc, hence not bein ale to login, can som

https://github.com/gyanu89/AutomatedRDP_Script

@gyanu89
Copy link

gyanu89 commented Aug 5, 2021

You can try here u may save ur current id password in ps file and call same file again and again only server name need to enter.
https://github.com/gyanu89/AutomatedRDP_Script

@jdforsythe
Copy link
Author

connect.ps1, is failing to load the password to mstsc, hence not bein ale to login, can someone please suggest anything?

Hello,

I'm actually facing the same issue. i used the code as above. The script is launching an RDP but unable to enter the password in it. Can you let me know how you fixed this issue?

I think the key from @gyanu89's code is that you need to wipe the old credentials from cmdkey first, so try adding this line to the beginning of the script:

cmdkey /list | ForEach-Object{if($_ -like "*target=TERMSRV/*"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}

@Edineidf
Copy link

cmdkey /generic:IP_PUBLIC /user:DOMAIN\USER /pass:"PASS"
mstsc.exe /v: IP_PUBLIC
exit

@Cloud7Paradox
Copy link

what if i want to start another ps script located in the remote server in which Iogged in with this script ? can anyone please support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment