Remotly launch a batch file on a Windows computer

Here’s a tip for remotly executing a command on a Windows computer in your network (supposing the target computer has allowed remote connections -NDMyself: this is to be checked…-). Suppose you want to run a batch file on a Windows computer called \\targetMachine. Suppose the batch file to execute is stored on a shared drive. Suppose a user on the target machine is created with login “toto” & password “myPassword”. Then the following batch executed from any computer in the network will run the test.bat file on the computer named “targetMachine”

Here’s the code:
set machine=targetMachine
set file=\\eici01\user\FMA\test.bat
set user=toto
set userPwd=myPassword
set executeAs=%user%
set executeAsPwd=%userPwd%
schtasks /Create /S %machine% /U %user% /P %userPwd% /RU %executeAs% /RP %executeAsPwd% /SC ONCE /TN install /TR %file% /ST 00:00:00
schtasks /Run /S %machine% /U %user% /P %userPwd% /TN install

(tested on Windows XP SP2)

  • Updated on 2009-07-01, thanks to Sasquatch, corrected “set executeAsPwd=%user%” as “set executeAsPwd=%userPwd%”
  • Updated on 2009-12-26, thanks to Neiv, correctly renamed the targetMachine in the code