Posts Tagged ‘daily task’

Creating scheduled tasks with schtasks.exe on Windows 7

Tuesday, April 24th, 2012

In Windows 7, it’s easy to create scheduled tasks. You can either use Task Scheduler under Programs -> Accessories -> System Tools or you can use schtasks.exe in the cmd. schtasks.exe can be scripted and most of the commands and options that are available in Task Scheduler are also available in schtasks.exe

In my particular case, I wanted to create a task that runs EVERYDAY in a 5 MINUTE INTERVAL from 07:00 to 21:30. Although schtasks.exe provides some examples, I promptly chose the wrong example to edit and whatever I tried didn’t seem to work.

After a lot of fiddling and calling up a good friend’s advice, this command line did the trick (should all be on one line):

schtasks /create /ru “system” /sc daily  /tn “your task name” /tr “‘%programfiles%\path\to\your\executable.exe'” /st 07:05 /sd 01/01/2012 /du 14:30 /ri 10 /f

What does this do?

Start a cmd as administrator and the above command line will create a task called “your task name”.
The task will be created and run:
-in system context (ru = run as user)
-daily from 07:05 (sc = schedule, st = starting time) until 21:30 (du = duration – 14.5 hours)
-in a 10 minute interval (ri = run interval in minutes)
-using the provided executable (tr = taskrun). Note that you can use environment variables that have to be escaped using single quotes.
-forcing the task e.g. if a task with the same name already exists, it will be overwritten

Where did I go wrong:

The help in schtasks provides some examples. My mistake was the following though:
If you choose /sc minute, you’re stuck on one particular day because you have to provide a /st and an /et (end time). The end time will always default to the provided time and the day the task was created and only that day. E.g. if you create a task running from /st 07:00 to /et 14:00, schtasks.exe will translate this to “from 07:00 <today> to 14:00 <today>” and only run it on that particular day and then never again.

Notable absent option in schtasks.exe:
In the Task Scheduler gui, a task can be created/optimized for Vista/W2k8 or Win7/W2k8_R2.
This options changes the tag “Task version” from version=”1.2″ to version=”1.3″ in the XML file of that task. In schtasks.exe, this option is not available but it doesn’t seem to make a difference at all.

HTHS
m.