Site Loader
Auckland, New Zealand
I was working on a project where most of my task in automation testing should be done with one single click or even not (it should be done on a scheduled basis), while this is the requirement, I had a task to connect with a remote machine and initiate a task in that machine. I was googling for the solution hunting to resolve the problem, in order words, I was searching for the best possible solution to resolve the problem to do so. Finally I found the solution.
To perform task in one single click or doing the same on a scheduled task, I choose batch file will be the best possible way, since it run a series of commands along with calling some other external applications too. And to connect to a remote machine and run certain execution in that particular machine, I choose “Powershell” Powershell is the scripting language introduced by Microsoft and has many features which performs operation in Windows OS as command line as we do the same in UI (User Interface). Powershell is fast and easy to learn, hence I thought the operation can be best achieved with it.
Solution 1
The easiest way is to save the remote machine connection file with credential and calling it via command prompt.
  1. First save the remote desktop file by opening remote desktop from ProgramàRun and type mstsc
  2. Now enter machine details like IP address or machine name and User name and Password.
  3. Click “Save As” button and save the remote desktop connection file in C:\ with name Remotemachine.RDP
You can invoke the RDP file from either command prompt or powershell For command prompt the command is very straight forward, just type the file name
Using powershell the command is
Invoke-Item "C:\Remotemachine.RDP"
  This solution will work fine, but the problem is, if we need to connect multiple machines dynamically, the solution will be hard to maintain all the RDP files. Hence the smart solution is writing a powershell script as shown below
Solution 2
function Connect-Mstsc {


This command dot sources the script to ensure the Connect-Mstsc function is available in your current PowerShell session
Connect-Mstsc -computername Mycomputer -UserName mymachine\executeauto -password Idonttellpasswords 

param (











process {

foreach ($Computer in $ComputerName) {

$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:$Password"

$Process.StartInfo = $ProcessInfo


$ProcessInfo.FileName = "$($env:SystemRoot)\system32\mstsc.exe"

$ProcessInfo.Arguments = "/v $Computer"

$Process.StartInfo = $ProcessInfo




Now pass the command line in powershell as shown below
Connect-Mstsc -computername Mycomputer -UserName mymachine\executeauto -password Idonttellpasswords
You can find more about the code shown above from the original author of code  from here, thanks to Jaap Brasser. I hope this chapter will be useful. Thanks, Karthik KK

Post Author: Karthik kk

Leave a Reply

Your email address will not be published. Required fields are marked *