MSTSC auto login command line
If You are searching for a command to connect MSTSC or RDP session automatically without asking user name and password then you are at right place, This is very helpful in the environment where you are supporting number of servers and putting user name and password each time when you takeĀ MSTSC or RDP session. This script will work only if your system is having command cmdkey.exe which is by default available in Windows 7, Windows 2008 etc…
Description:
# is used for comment so the line couldn’t execute.
#Script for RDP or MSTSC auto login
#———————————-
code used to create connect function in a script file.
function connect{
This will verify that computer name is passed with the connect function as an argument example connect server1.
param ([Parameter(Mandatory=$true)]
[string[]]$Computer )
A variable to hold domain and username to access remote computer, make changes according to your domain name and your user id.
$User = “Domain\UserName”
A condition to verify whether pass.txt exist in the current folder if it is not exist than password needs to be input with read-host command as a secure string and will be saved to pass.txt in encrypted form.
if (-not (Test-Path .\Pass.txt))
{
read-host -assecurestring “Enter Password” | convertfrom-securestring | out-file .\Pass.txt
}
Reading password through cat which is a aliasĀ of get-content
$password = cat .\pass.txt | convertto-securestring
New powershell object is created to convert $password in plain text in the next statement.
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $User, $password
$pass = $cred.GetNetworkCredential().Password
A code to start executing command with arguments collected by object $processinfo like in below code cmdkey will be executed to store computer name, User Name & password.
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
$Process = New-Object System.Diagnostics.Process
$ProcessInfo.FileName = “$($env:SystemRoot)\system32\cmdkey.exe”
$ProcessInfo.Arguments = ” /generic:TERMSRV/$Computer /user:$User /pass:$Pass”
$Process.StartInfo = $ProcessInfo
$Process.Start()
This code will start mstsc session and will take username & password from windows store created by above command.
$ProcessInfo.FileName = “$($env:SystemRoot)\system32\mstsc.exe”
$ProcessInfo.Arguments = ” /v $Computer”
$Process.StartInfo = $ProcessInfo
$Process.Start()
Now at last it will delete stored username password with the same command cmdkey.exe
$ProcessInfo.FileName = “$($env:SystemRoot)\system32\cmdkey.exe”
$ProcessInfo.Arguments = “/delete TERMSRV/$Computer”
$Process.StartInfo = $ProcessInfo
$Process.Start()
How to use: Open notepad then copy below script text and paste it in notepad, Save notepad file on the name of Connectfunction and extension .ps1 a powershell script file. make sure notepad file is not saved as a .txt file extension, if it is then it will not execute.
Open Powershell and Run command (Get-Executionpolicy) to check execution policy of the system where this script need to be executed. if it is unrestricted then you can execute the same or else need to change execution policy with the powershell command (Set-Executionpolicy unrestricted) , A warning message will come press y to set the execution policy.
Before executing this script make sure you have changed $user variable value with your domain & username in the same format.
in powershell go to the location where the script is saved and execute the same by typing .\Connectfunction.ps1
Now connect function is ready to use as below. First time it will ask for password after that it will save into pass.txt as a secure text (encrypted form) and use whenever required.
Note: secure password (encrypted text) can be converted into plain text therefore delete this file incase you are using shared system or default login access.
#Script for RDP/MSTSC auto login
#———————————-
function connect{
param ([Parameter(Mandatory=$true)]
[string[]]$Computer )
$User = “Domain\UserName”
if (-not (Test-Path .\Pass.txt))
{
read-host -assecurestring “Enter Password” | convertfrom-securestring | out-file .\Pass.txt
}
$password = cat .\pass.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $User, $password
$pass = $cred.GetNetworkCredential().Password
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
$Process = New-Object System.Diagnostics.Process
$ProcessInfo.FileName = “$($env:SystemRoot)\system32\cmdkey.exe”
$ProcessInfo.Arguments = ” /generic:TERMSRV/$Computer /user:$User /pass:$Pass”
$Process.StartInfo = $ProcessInfo
$Process.Start()
$ProcessInfo.FileName = “$($env:SystemRoot)\system32\mstsc.exe”
$ProcessInfo.Arguments = ” /v $Computer”
$Process.StartInfo = $ProcessInfo
$Process.Start()
Start-Sleep -s 20
$ProcessInfo.FileName = “$($env:SystemRoot)\system32\cmdkey.exe”
$ProcessInfo.Arguments = “/delete TERMSRV/$Computer”
$Process.StartInfo = $ProcessInfo
$Process.Start()
}
#———————————-
Example:
Connect ServerName