Scheduling Tasks with Schtasks
With Schtasks, you can perform the same task scheduling
operations as with the Scheduled Task Wizard. Any tasks you create using
Schtasks are displayed as scheduled tasks in the Scheduled Tasks folder and can
be managed from the command line or from the graphical user interface (GUI).
Schtasks has several different sets of subcommands and is one of
the more complex utilities available at the command line. The sections that
follow discuss each of the following subcommands:
-
Schtasks /Create Used to create scheduled
tasks
-
Schtasks /Change Used when you want to
change the properties of existing tasks
-
Schtasks /Query Used to display scheduled
tasks on the local or named computer
-
Schtasks /Run Used to start a scheduled
task immediately
-
Schtasks /End Used to stop a running
task
-
Schtasks /Delete Used to remove scheduled
tasks that are no longer wanted
Creating
Scheduled Tasks with Schtasks /Create
With Schtasks /Create, you can create one-time-only tasks,
recurring tasks, and tasks that run based on specific system events, such as
logon and startup. The basic syntax for defining these types of tasks is as
follows:
schtasks /create /tn TaskName /tr TaskToRun /sc ScheduleType [/mo
Modifier]
where
TaskName sets the task name string,
TaskToRun specifies the file path to the command-line utility
or script that you want to run,
ScheduleType specifies the
run schedule, and
Modifier is an optional value that
modifies the run schedule based on the schedule type. Any tasks you create using
this syntax are created on the local computer and use your user permissions.
Further, if you don’t provide your account password, you are prompted for your
user password when you create tasks.
Valid values for ScheduleType are shown in
Table 4-1. Note the usage and
modifiers that the various schedule types accept. We will discuss each schedule
type and modifier in detail later in the chapter. Note the following as
well:
Table 4-1: Schedule Types for Schtasks
/Create
Schedule Type
|
Description
|
Modifier Values
|
MINUTE
|
Task runs at a specified interval in minutes. By default,
tasks run once a minute.
|
/mo 1-1439; the number of minutes between each run of the
task. The default modifier is 1.
|
HOURLY
|
Task runs at a specified interval in hours. By default,
tasks run once an hour.
|
/mo 1-23; the number of hours between each run of the task.
The default modifier is 1.
|
DAILY
|
Task runs every day or every N days.
By default, tasks run once a day.
|
/mo 1-365; the number of days between each run of the task.
The default modifier is 1.
|
WEEKLY
|
Task runs every week or every N weeks,
on designated days. By default, tasks run once a week on Mondays.
|
/mo 1-52; the number of weeks between each run of the task.
Optionally, use /d to specify the days of the week to run. Use MON, TUE, WED,
THU, FRI, SAT, and SUN to specify days. * for every day of the
week.
|
MONTHLY
|
Task runs every month or every N
months on designated days. By default, tasks run the first day of every month.
|
/mo 1-12; the number of months between each run of the task.
Optionally, use /d MON-SUN; sets day of the week to run during the month. Use *
to have the task run every day.
|
|
2nd monthly variant for specific day of month. Use /mo and
/m, or /m and /d.
|
/mo LASTDAY; last day of month /m
JAN, FEB, ..., DEC; sets the month(s). /d 1-31; day
of month
|
|
3rd monthly variant for specific week of the month.
|
/mo FIRST | SECOND | THIRD | FOURTH | LAST; sets week of
month. /d MON-SUN; sets day of week./m JAN, FEB, ...,
DEC; sets month(s).
|
ONCE
|
Task runs once at a specified date and time.
|
—
|
ONSTART
|
Task runs whenever the system starts.
|
—
|
ONLOGON
|
Task runs whenever a user logs on.
|
—
|
ONIDLE
|
Task runs whenever the system is idle for a specified period
of time.
|
/i 1-999; the number of minutes the system has to be idle
before the task starts.
|
-
Days of the week can be entered in a comma-separated list,
such as Mon, Wed, Fri, or with a hyphen to specify a sequence of days, such as
Mon-Fri for Monday through Friday.
-
Months of the year can be entered in a comma-separated list,
such as Jan, Mar, Jun, or with a hyphen to specify a sequence of months, such as
Jan-Jun for January through June.
-
With week of the month, you can only enter one value, such
as FIRST or LAST.
To see how Schtasks /Create can be used, consider the following
examples:
Task runs once immediately and then doesn’t run
again:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc once
Task runs when the system starts:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc onstart
Task runs whenever the system is idle for more than
10 minutes:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc onidle
/i 10
Task runs every 15 minutes on the local
computer:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc minute
/mo 15
Task runs every five hours on the local
computer:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc hourly
/mo 5
Task runs every two days on the local computer:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc daily
/mo 2
Task runs every two weeks on Monday (the default run
day):
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc weekly
/mo 2
Task runs every week on Monday and Friday:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc weekly
/d mon,fri
Task runs on the first day of every month:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc monthly
Task runs on the fifth day of every other month:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc monthly
/mo 2 /d 5
Task runs the last day of every month:
schtasks /create /tn "SysChecks" /tr c:\scripts\sysch.bat /sc
monthly/mo lastday
Task runs the first Monday of April, August, and
December:
schtasks /create /tn "SysChecks" /tr c:\scripts\sysch.bat /sc
monthly /mo first /d mon /m apr,aug,dec
When the path of the specified task includes a space, enclose the
file path in double quotation marks as shown in the following example:
schtasks /create /tn "SysChecks" /tr "c:\My Scripts\sch.bat" /sc
onstart
If you do not enclose the file path in quotation marks, an error
will occur when Schtasks attempts to run the task. Further, if you want to pass
arguments to a program, utility or script, simply follow the Task To Run file
path with the arguments you want to use. Any argument that contains spaces
should be enclosed in quotation marks so that it is properly interpreted as a
single argument rather than multiple arguments. Here are examples:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat 1 Y LAST /sc
onstart
schtasks /create /tn "SysChecks" /tr "c:\My Scripts\sch.bat" Y N /sc
onstart
schtasks /create /tn "SysChecks" /tr "c:\My Scripts\sch.bat" "Full
Checks"
You can also schedule tasks for remote computers as well as tasks
that should run with different user permissions. The key detail to remember when
scheduling tasks on remote computers is that the computer you are using should
be in the same domain as the remote computer or in a domain that the remote
computer trusts. To do this, you must use the expanded syntax which includes the
following parameters:
/s Computer /u [Domain\]User [/p Password]
where Computer is the remote computer name
or IP address, Domain is the optional domain name in which
the user account is located, User is the name of the user
account whose permissions you want to use, and Password is
the optional password for the user account. If you don’t specify the domain, the
current domain is assumed. If you don’t provide the account password, you are
prompted for the password.
|
Tip |
If you want a task to run only when a specific user is
logged on, use the optional /It parameter, which specifies that the task should
run only when the user who owns the task is logged on. The /It parameter is only
valid for Windows Server 2003. It cannot be used with Windows
XP.
|
To see how the computer and user information can be added to the
syntax, consider the following examples:
Use the account adatum\wrstanek when setting
permissions for the task on the local computer:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc onstart
/s mailer01 /u adatum\wrstanek
Set the remote computer as mailer01 and the account
to use as adatum\wrstanek:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc onstart
/s mailer01 /u adatum\wrstanek
Finally, if desired, you can add specific start times and dates,
as well as end times and dates, using
-
/st StartTime, where StartTime is in
24-hour clock format (HH:MM), such
as 15:00 for 3 P.M.
-
/et EndTime, where EndTime is in
24-hour clock format (HH:MM), such
as 15:00 for 3 P.M. Used only with /sc minute and /sc hourly. Applies to Windows
Server 2003 only.
-
/du Duration, where Duration is the
number of hours and minutes to run, in the form HHHH:MM. Used only with /sc minute and /sc hourly. Applies to
Windows Server 2003 only.
-
/sd StartDate, where StartDate is the
start date using the default system format for dates, such as MM/DD/YYYY.
-
/ed EndDate where EndDate is the end
date using the default system format for dates, such as
MM/DD/YYYY.
|
Tip |
If you specify an end date or time, you can also specify the
/Z parameter, which tells the Task
Scheduler to delete the task upon completion of its schedule. This applies only
to Windows Server 2003.
|
To see how specific start times and dates, as well as end times
and dates, can be used, consider the following examples:
Start the hourly task at midnight:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc hourly
/st 00:00
Start the hourly task at 3 A.M. and stop it at 7
A.M.:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc hourly
/st 03:00 /et 07:00
Start the weekly task at 3 A.M. on February 20,
2004:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc weekly
/st 03:00 /sd 02/20/2004
Start the weekly task at 3 A.M. on February 20, 2004
and end at 2:59 A.M. on March 15, 2004:
schtasks /create /tn "SysChecks" /tr c:\scripts\sch.bat /sc weekly
/st 03:00 /sd 02/20/2004 /et 02:59 /ed 03/15/2004
|
Note |
Date and time formats are determined by the Regional And
Language options settings used by the computer. In these examples, the date
format preference is English (United States).
|
Changing
Scheduled Tasks with Schtasks /Change
You use Schtasks /Change to change key parameters associated
with scheduled tasks. The basic syntax of Schtasks /Change is
schtasks /change /tn TaskName ParametersToChange
where TaskName is the name of the task you
want to change and Parameters-
ToChange are the parameters you
want to change. Parameters you can work with include
-
/ru Domain\User
changes the user to run the task as, such as /ru adatum/ wrstanek.
-
/rp Password sets the password for the
previously specified or newly designated run as user account.
-
/tr TaskToRun changes the command-line
utility or script that is run for the named task.
-
/st StartTime sets the start time for
minute or hourly tasks. This applies to Windows Server 2003 only.
-
/ri Interval sets the run interval in
minutes. This applies to Windows Server 2003 only.
-
/et EndTime sets the end time for
minute or hourly tasks. This applies to Windows Server 2003 only.
-
/du Duration sets the number of hours
and minutes to run the task. Minute and hourly tasks only. This applies to
Windows Server 2003 only.
-
/sd StartDate sets the start date for
the task. This applies to Windows Server 2003 only.
-
/ed EndDate sets the end date for the
task. This applies to Windows Server 2003 only.
-
/k specifies that the task should not be started again when
the end time or duration interval is reached, but it doesn’t stop the task if it
is already running (the current run will be the last one). This applies to
Windows Server 2003 only.
-
/it specifies that the task should run only when the user
who owns the task is logged on. This applies to Windows Server 2003
only.
To see how you can change tasks, consider the following
examples:
Change the script that is run:
schtasks /change /tn "SysChecks" /tr c:\scripts\systemchecks.bat
Change the run as user and password:
schtasks /change /tn "SysChecks" /ru adatum\hthomas /rp gophers
Change task to start weekly at 7 A.M. on March 1,
2004 and end at 6:59 A.M. on March 30, 2004:
schtasks /change /tn "SysChecks" /st 07:00 /sd 03/01/2004 /et 06:59
/ed 03/30/2004
|
Note |
As mentioned previously, date and time formats are
determined by the Regional And Language options settings used by the computer.
Here, the date format is English (United States).
|
When you change a task, Schtasks displays a message that states
whether the changes succeeded or failed, such as
SUCCESS: The parameters of the scheduled task "SysChecks" have been
changed.
If you are working with a remote computer or aren’t logged in with
a user account that has permission to change the task, you can specify the
computer and account information as necessary. The syntax is
schtasks /change /tn TaskName /s Computer /u [Domain\]User [/p
Password]
where Computer is the remote computer name
or IP address, Domain is the optional domain name in which
the user account is located, User is the name of the user
account whose permissions you want to use, and Password is
the optional password for the user account. If you don’t specify the domain, the
current domain is assumed. If you don’t provide the account password, you are
prompted for the password.
In the following example, the remote computer is mailer1 and the
user account that has authority to change the SysChecks task is wrstanek’s
Adatum domain account:
schtasks /change /tn "SysChecks" /tr c:\scripts\systemchecks.bat
/s mailer01 /u adatum\wrstanek
Because a password isn’t specified, Schtasks will prompt for one.
In Windows Server 2003 only, you can also quickly enable or disable tasks by
name. Use the following syntax to enable tasks:
schtasks /change /tn TaskName /enable
Use this syntax to disable tasks:
schtasks /change /tn TaskName /disable
where TaskName is the name of the task you
want to enable or disable, such as
schtasks /change /tn "SysChecks" /disable
Querying for
Configured Tasks with Schtasks /Query
You can quickly determine what tasks are configured on a
computer by typing schtasks query at the command prompt and,
as necessary for a remote computer, you can specify the computer and the account
information needed to access the computer using the form:
schtasks /query /s Computer /u [Domain\]User [/p Password]
where Computer is the remote computer name
or IP address, Domain is the optional domain name in which
the user account is located, User is the name of the user
account with appropriate access permissions on the remote computer, and Password is the optional password for the designated user
account.
In the following example, the remote computer is mailer1 and the
user account is wrstanek’s Adatum domain account:
schtasks /query /s mailer01 /u adatum\wrstanek
Because a password isn’t specified, Schtasks will prompt for one.
The basic output of Schtasks /Query is in table format and
provides TaskName, Next Run Time, and Status columns. You can also format the
output as a list or lines of comma-separated values using /Fo List or /Fo Csv,
respectively. The list output works best with the /V (verbose) parameter, which
provides complete details on all task properties and which can be used as shown
in the following example:
schtasks /query /s mailer01 /u adatum\wrstanek /fo list /v
Another parameter that is useful is /Nh, which specifies that
table- or CSV- formatted output should not have headings.
|
Tip |
You may wonder why you’d want to use the various formats.
It’s a good question. I recommend using the verbose list format (/Fo List / V)
when you want to see all details about tasks configured on a system and when you
are troubleshooting, and I recommend using comma-separated values when you want
to store the output in a file that may later be exported to a spreadsheet or
flat-file database. Remember you can redirect the output of Schtasks to a file
using output redirection (> or >>).
|
Running Tasks
Immediately with Schtasks /Run
You can run a task at any time using
schtasks /run /tn TaskName
where TaskName is the name of the task you
want to run, such as
schtasks /run /tn "SysChecks"
Running a task does not affect its schedule and does not change
the next run time for the task. If the task can be successfully started, you
should see a message stating so. Additionally, you can specify the name of the
remote computer on which the task is configured and as necessary the account to
run the task as, including an optional password. Here are examples:
schtasks /run /tn "SysChecks"/s 192.168.1.100
schtasks /run /tn "SysChecks"/s 192.168.1.100 /u adatum/wrstanek
|
Note |
If you specify a user and don’t provide a password, you will
be prompted immediately to enter the
password.
|
Stopping Running
Tasks with Schtasks /End
You can stop a task at any time using
schtasks /end /tn TaskName
where TaskName is the name of a task that is
currently running and should be stopped, such as
schtasks /end /tn "SysChecks"
The task is only stopped if it is running and if successful, the
output message should be similar to the following:
SUCCESS: The scheduled task "SysChecks" has been terminated
successfully.
You can also specify the name of the remote computer on which the
task is configured and as necessary the account with authority to stop the task,
including an optional password, such as:
schtasks /end /tn "SysChecks"/s 192.168.1.100
Or
schtasks /end /tn "SysChecks"/s 192.168.1.100 /u adatum/wrstanek
Because a password isn’t specified, Schtasks will prompt for
one.
Deleting Tasks
with Schtasks /Delete
You can delete tasks by name on local and remote computers
using the syntax
schtasks /delete /tn TaskName [/s Computer /u [Domain/]User [/p
Password]]
where TaskName is the name of a task that
should be deleted and the rest of the parameters optionally identify the remote
computer, the user account to use when deleting the task and the password for
the account, such as
schtasks /delete /tn "SysChecks"
or
schtasks /delete /tn "SysChecks"/s 192.168.1.100 /u adatum/wrstanek
/p frut5
|
Note |
If you specify a user name and don’t provide a password, you
will be prompted immediately to enter the
password.
|
After entering the Schtasks /Delete command, you should see a
warning asking you to confirm that you want to remove the task. Press the
appropriate letter on your keyboard. If you don’t want to see a warning prompt
use the /f parameter, such as
schtasks /delete /tn "SysChecks" /f
Here, you force Schtasks to delete the task without a warning.
In addition, if you want to delete all scheduled tasks on the
local computer or the specified remote computer, enter asterisk (*) as the task
name, such as
schtasks /delete /tn *
Confirm the action when prompted.