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
pause

(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

36 thoughts on “Remotly launch a batch file on a Windows computer

  1. when I use this batch, I get an error saying:

    WARNING:
    The Scheduled task “install” has been created, but may not run because
    the account information could not be set.

    Can you help me please?

  2. Stephen, it sounds to me like a user rights problem. Is your user limited ? Can you try with the Administrator user (or any user with more rights) ?
    By the way, despite the warning message, is the remote task correctly executed by the last line in the script ?

  3. There is a slight error in the original script.

    The line that reads: set executeAsPwd=%user%

    Should be: set executeAsPwd=%userPwd%

    Once I made this change it worked perfectly for how I need it. I also added in a separate wait command that paused the script for 10 seconds, and then added in the like below to delete the scheduled task back out after it runs the task.

    schtasks /Delete /S %machine% /F /TN install

    Thanks for the suggestion on creating a scheduled task remotely to run a batch file, I don’t think I would have thought of that.

  4. I’ve been looking for something like this for quite some time, but it doesn’t seem to be working for some reason. Although it’s not explicitly stated, I assume the code itself is to be placed into a separate batch file, even though it is referencing another batch file called test.bat?

    Also, I’m not quite certain I understand the second line of the file, namely:

    set file=\\eici01\user\FMA\test.bat

    From what I gather, this is a file/path located on a shared drive independent of the initiating machine or the remote machine? What does eici01 refer to? is “user” to be switched out with the actual user name of the drive that hosts the file? What does FMA refer to? Is that supposed to be changed?

    Can I just place the file on the same initiating machine that launches the file? In other words, say I place this code into a file called launch.bat which resides on in a “maintenance_files” folder on the root of c:\ of my server machine (c:\maintenance_files). Further, say I want this batch file to launch the test.bat file referenced in the “set file” line above. Does the test.bat file reside on the “receiver” machine (the client) or the “sender” machine (the server)? Could you perhaps provide a concrete example that would make this clearer? Thanks.

  5. Hi Neiv, thanks for your comment. I realize my exemple was not very well explained !
    So to answer all your questions:
    1- yes, the code given must be in a separate batch file, on any computer with network access to computer “targetMachine”.
    2- “set file=\\eici01\user\FMA\test.bat” was a path to the file to be run remotly. This file can be either remote (as in my exemple) or local (it would read like set file=c:\xxxxx\test.bat). So you are right, it can be independant of the initiating or remote machine.
    3- \user\FMA\ was only the path to the file. But the use of “user” for the directory name was misleading… So I changed it to “A\Path\To\A\Shared\File” now 😉
    4- In fact, my code only instruct the “targetMachine” computer to run a batch file. As long as targetMachine can access this batch file (locally or remotly on a shared folder), you can instruct it to run this file with my code.

    So I would advise you to begin by testing your batch file on the targetMachine computer. Log onto targetMachine, then try to run your batch file (test.bat). If it is successful, then try to make it run on targetMachine by initiating it from another computer on the network with my code. In your exemple, if you place both my code batch and the target test.bat in your maintenance_files, you have to make sure that “maintenance_files” is a shared directory that the “targetMachine” can access on the network. The best is to first try with “targetMachine” local filesystem I think.

    Hope this helps !

  6. Francois,

    This is fantastic! I was having issues with psexec related to remote printer installs. This worked great, and is much faster than psexec!

    Thank you!

  7. Francois,

    Brilliant. I can restore my son’s OS from an image stored in my computer. I no longer have to physically access his computer.

  8. Hi Francois,

    I want to create one batch file which can be run from any machine connected at the network. if the user enter valid Machine name it should take user to that machine, if not, it should display a msg “Please enter valid Machine name”.

    Please help me how can i create that file.

  9. Have not tested this script yet, but I believe that the REMOTE execution will require the complete path, like this:
    set file=\\eici01\c$\user\FMA\test.bat

    not this: set file=\\eici01\user\FMA\test.bat

  10. I get this error when running it…

    ERROR: specified task name “install” does not exist in system.

    I checked event log error (Task Scheduler/Operational) in the remote sytem and I got this…

    User “SYSTEM” deleted Task Scheduler task “\install”

    What do you think is wrong with my remote system?

  11. I will like to make a network using batch files and i need help to make it. can u send me some batch codes.

  12. Hi,
    I ran the script it worked fine.But i am getting below message
    WARNING: Task may not run because /ST is earlier than current time.
    WARNING: The task name “install” already exists. Do you want to replace it (Y/N)

    So each time i have to manually enter it.Is there anyway can we resolve it.

    1. Add /y at the end of the command (schtasks /Create /S %machine% /U %user% /P %userPwd% /RU %executeAs% /RP %executeAsPwd% /SC ONCE /TN install /TR %file% /ST 00:00:00 /y)

  13. i want to run a batch file in local system which should trigger a batch file in remote system.how can we do it

  14. I get “The request is not supported” after both schtasks call. I am logged in as Administrator and have all the rights.

  15. Thanks for the link, I enabled the remote scheduled task management in my FW and I’m able to run schtasks now, but I get a different error:

    ERROR: A specified logon session does not exist. It may already have been terminated.

  16. when i run created batch file for other computer i get message like this
    C:\>echo off
    ERROR: The network path was not found.
    ERROR: The network path was not found.
    Press any key to continue . . .
    ERROR: The network path was not found.

  17. Hello there! This article couldn’t be written much better! Reading through this post reminds me of my previous roommate! He constantly kept preaching about this. I am going to forward this post to him. Pretty sure he’s going to have
    a very good read. Thanks for sharing!

  18. I am getting access denied error and i gave computer name as IP address of that PC,
    could you provide me the solution

    1. HI,
      I am getting the same error. Did you find a workaround for this issue?
      Thanks!

  19. Hi all,
    The main script is creating the task in my remote system. But my batch file is not opening.
    Even i checked “Last run result” from Task Scheduler (Remote system) its states is “An instance of this task is already running”. But the batch file is not opening at all . Please help me out.

  20. Hi all ,
    Now i am figure out i am getting some error and warnings in Task Scheduler history tab.

    Task Scheduler failed to start “\install” task for user ” UR\Administrator”. Additional Data: Error Value: 2147750687.

    Task Scheduler did not launch task “\install” because instance “{74bf411e-8fdb-4bc8-8028-30f9356a2f75}” of the same task is already running.

    May i know why its returning these errors and warnings ??

  21. i need to run batch file to install application in remote computers in network from single PC by batch file.
    Sample code :
    set machine=machinename
    set file=\\networkpath\1.exe – file name
    set user=Domainname\username
    set userPwd=domain pwd
    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
    pause

    error
    ——-
    C:\Users\*****\Desktop>schtasks /Create /S Hostname /U domainname\username /P $$$$ /RU msft.cbay\MM013851 /RP $$$$$ /SC ONCE /TN install /TR \\172.20.0.18\foldername\filename.exe /ST 00:00:00
    ERROR: Invalid argument/option – ‘Installation’.
    Type “SCHTASKS /CREATE /?” for usage.

    C:\Users\*******\Desktop>schtasks /Run /S Hostname /U domainname\username /P Psri@86 /TN install
    ERROR: The request is not supported.

    C:\Users\MM013851\Desktop>pause
    Press any key to continue . . .

    Getting an errror

  22. set machine=machinename
    set file=\\networkpath\1.exe – file name
    set user=Domainname\username
    set userPwd=domain pwd
    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
    pause

    error
    ——-
    C:\Users\*****\Desktop>schtasks /Create /S Hostname /U domainname\username /P $$$$ /RU msft.cbay\MM013851 /RP $$$$$ /SC ONCE /TN install /TR \\172.20.0.18\foldername\filename.exe /ST 00:00:00
    ERROR: Invalid argument/option – ‘Installation’.
    Type “SCHTASKS /CREATE /?” for usage.

    C:\Users\*******\Desktop>schtasks /Run /S Hostname /U domainname\username /P Psri@86 /TN install
    ERROR: The request is not supported.

    C:\Users\MM013851\Desktop>pause
    Press any key to continue . . .

  23. Hi While executing above command getting following Error “The request is not supported.”

    Plz help me out

  24. Hi,

    I am running batch file from remote server but I am getting below error.

    WARNING: Task may not run because /ST is earlier than current time.
    WARNING: The task name “install” already exists. Do you want to replace it (Y/N)
    ?

    As you suggested I have given /y at the end but it’s throws error as
    ERROR: Invalid argument/option – ‘/y’.
    Type “SCHTASKS /CREATE /?” for usage.

    Could you please help me.

    1. I had not approved it until now. Just I was not home ! 😉
      Regarding your question, sorry I have no windows computer anymore, even less more than one on the network so I can’t help you. If someone founds the answer, he is welcome to comment.

Leave a reply to srinivasan Cancel reply