17

I would like to have the console window kept open when running a scheduled task which executes a batch file. If I run it manually meaning execute the batch file the window stays open but through task scheduler it doesn't open but I can see the task is still running. I put a pause at the end to do this.

 @echo off 
 TITLE PROCESS_MGR
 tasklist /FI "IMAGENAME eq JOESMO.exe" | find /I "JOESMO.exe">nul &&(echo PROCESS 
 JOESMO.exe IS ALREADY RUNNING! 
 echo %DATE%
 echo %TIME%
 pause
 ) || (
 echo JOESMO PROCESS IS NOT RUNNING 
 cmd /c start "JOESMO.exe" "C:\Users\xxxx\Documents\
 Visual Studio 2010\Projects\Projects2013\JOESMO.exe"
 pause)

I found this suggestion cmd /k myscript.bat but having creating the task in task scheduler for windows server 2008 I am not sure where to apply this. I added /k to the add arguments box in edit action in task.

vbNewbie
  • 3,291
  • 15
  • 71
  • 155

5 Answers5

34

In the Scheduled Task dialog, just before the name of the batch file it's going to run (it's labeled Program/script. You now have something like:

myscript.bat

Change it to

cmd 

Add the following to the **Add Arguments (optional) entry:

/k "C:\My Batch File Folder\MyScript.bat"

Tested on my system (Win7 64-bit), and it worked perfectly. I'm looking at the open command window it created as I type this text. :-)

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • thanks for the response. I did that and got a question on whether I wish to run the task with the following arguments /k "c:\..bat". I said yes, tried it and the task was still running with no console window. – vbNewbie Nov 29 '13 at 14:11
  • Thanks so much. Got it to work..was a typo in the arguments field – vbNewbie Nov 29 '13 at 14:55
  • 1
    Is this still working on Windows 10? I'm having trouble with it. -- Oh, I see, the task must be configured to be run as the logged in user... – Daniel F Aug 16 '16 at 13:39
  • Windows Server 2012 R2: this will work but you have to make sure that "Run only when user is logged on" is selected on the General tab for your task. Then when you click Run the cmd window will open. – Perry Oct 28 '16 at 15:28
  • Worked for me in Windows Server 2003! – Alfredo Capobianchi Apr 11 '17 at 14:42
  • must turn on "Run only when user is logged on" on windows 10. with "run whether user is logged on or not" check it does not popup the command windows. But the task actually runs in the background. – Tony Apr 07 '21 at 16:05
4

Unfortunately Ken's solution didn't work for me on a Windows 2008 R2 Std server, I was able to launch an interactive window by modifying the scheduled tasks setting using schtasks.exe

In a command window I did the following command:

schtasks /Change /TN "My Task" /IT

However that does require you be logged in as the same user context in which the scheduled task is executing. So if your scheduled task is use the localsystem "taskaccount" then you will have to log into the system as the "taskaccount" user.

Oddly enough it worked when I manually run the task but it didn't pop for me when it kicked off at a scheduled time.

David Yu
  • 186
  • 2
  • 5
  • I tried to use 'schtasks' and it asked "Please enter the run as password for I entered in my Windows credentials and it said "ERROR: Access is denied." – Mark Ginsburg Aug 21 '18 at 22:08
3

Ken's answer didn't worked for me.

Found this way of doing :

in your BAT file (create one if you only have an EXE) :

start C:/Absolute/Path/To/MyScript.exe myScriptArg

works like a charm !

Note: In the scheduled task, you must check "Exec only if user is logged"

Apolo
  • 3,844
  • 1
  • 21
  • 51
0

Create a shortcut to the Batchfile and put that in the action. Worked for me

-1

I tried all of the above, but they did not work for me. Here is what I did to get this to work:

Platform Windows Server 2003 R2 SP2 ActivePERL v5.10.1

Steps

  1. Create DOS BATCH script -- this runs the actual program, ie, myscript.bat
  2. Create PERL script to call the DOS batch script, ie, myscript.pl
  3. myscript.pl is a 1-line script: system("e:\scripts\myscript.bat");
  4. Create scheduled task: perl myscript.pl

The DOS command prompt window now always opens up. And more importantly, the task now successfully runs and completes. NOTE: The scheduled task RunAs user is logged in to the server.