Scheduling a Task That Runs Only Once
The following command creates a task named “Run Once” and runs the batch file,
Mybatch.bat, located in the C:\MyFiles folder on the local computer.The task runs only
once, on June 6, 2006 at 6:00 P.M. Note that the task name has a space in it and is enclosed
in quotation marks:
Schtasks /create /tn "Run Once" /tr C:\MyFiles\Mybatch.bat /sc once /sd 06/01/06 /st 18:00
If you want the schtasks utility to delete the task when it has completed, you can use the
/z parameter.This command is supported only in Windows Server 2003 and not on the
Windows XP operating system.
Scheduling a Task That Runs When a User Logs On:
The following command creates a task named LogonTask and runs it whenever a user logs on to the system.The task runs the file named Dskchek.bat from the C:\Utilities folder on the local computer. Note that the /sc logon is used as the schedule type option.
Sctasks /create /tn LogonTask /tr C:\Utilities\Dskchek.bat /sc logon
Scheduling a Task That Runs When the System Starts
The following command creates a task named “Startup Task” and runs it every time the system starts or is restarted.The task runs a file named Dskchek.bat in the C:\Utilities folder 80 Chapter 3 • Managing Scheduled Tasks on a remote computer named HRSvr02. Note that the /s Computer parameter is used to specify a remote computer and that the schedule type option is used as /sc onstart.
Schtasks /create /tn "Startup Task" /tr C:\Utilities\Dskchek.bat /sc onstart /s HRSvr02
Scheduling a Task That Runs When the System Is Idle for 15 Minutes
The following command creates a task named “Idle Task” and runs a file named Mybatch.bat located in the C:\MyFiles folder on the local computer. Note that the schedule type option is specified as /sc onidle and the /i 15 parameter specifies that the system should be idle for at least 15 minutes before the task starts.
Schtasks /create /tn "Idle Task" /tr C:\MyFiles\Mybatch.bat /sc onidle /i 15
Scheduling a Task That Runs Every 10 Minutes
The following command creates a task named MinTask and runs it every 10 minutes on the local computer.The task runs a batch file named Mybatch.bat from the C:\MyFiles folder on the local computer. Note that the schedule type option is specified as /sc minute and the interval is set at 10 minutes using the /mo 10 modifier.
Schtasks /create /tn MinTask /tr C:\MyFiles\Mybatch.bat /sc minute /mo 10
Now, check the following command, which creates a task to run every 30 minutes
starting at 8:00 every night and ending the next day at 7:00 A.M. This command is useful for scheduling a task that should start after office hours and stop before the office reopens.The /st and /se parameters have been used for start and end time, respectively. In addition to this, you can set the /k parameter to stop the task at 7:00 A.M. if it is still running.
Schtasks /create /tn MinTask /tr C:\MyFiles\Mybatch.bat /sc minute /mo 30 /st 30:00 /et 07:00
Scheduling a Task That Runs Every Four Hours
The following command creates a task named MinTask and runs it every four hours on the local computer.The task runs a batch file named Mybatch.bat from the C:\MyFiles folder on the local computer. Note that the schedule type option is specified as /sc hourly and the interval is set at four hours using the /mo 4 modifier.
Schtasks /create /tn MinTask /tr C:\MyFiles\Mybatch.bat /sc hourly /mo 4
Scheduling a Task That Runs Every Day
The following command creates a task named “Daily Task” and runs it every day on the local computer.The task runs a batch file named Mybatch.bat from the C:\MyFiles folder on the local computer. Note that the schedule type option is specified as /sc daily and /sd and /ed parameters are used to specify the start and end dates, respectively. Since we are not using the /mo modifier in this command, the task will run every day with a default value of 1.
Schtasks /create /tn "Daily Task" /tr C:\MyFiles\Mybatch.bat /sc daily /sd 03/01/2006 /ed 09/30/2006
Scheduling a Task That Runs Every 10 Days
The following command creates a task named 10DayTask and runs it every 10 days on the local computer.The task runs a batch file named Mybatch.bat from the C:\MyFiles folder on the local computer. Note that the schedule type option is specified as /sc daily with the modifier /mo 10, which means every 10 days.The /sd and /ed parameters are used to specify the start and end dates, respectively.The /st parameter specifies the time when the job should start.
Schtasks /create /tn 10DayTask /tr C:\MyFiles\Mybatch.bat /sc daily /mo 10 /sd 03/01/2006 /st 12:00 /ed 09/30/2006
Scheduling a Task That Runs Every Week on Wednesday
The following command creates a task named WedTask and runs it every week on Wednesday on the local computer.The task runs a batch file named Mybatch.bat from the 82 Chapter 3 • Managing Scheduled Tasks C:\MyFiles folder on the local computer. Note that the schedule type option is specified as /sc weekly with the modifier /d wed, which means every Wednesday.
Schtasks /create /tn WedTask /tr C:\MyFiles\Mybatch.bat /sc daily /d wed
Scheduling a Task That Runs Every Alternate Week on Monday and Friday The following command creates a task named 2WeekTask and runs it every alternate week on Monday and Friday on the local computer.The task runs a batch file named Mybatch.bat from the C:\MyFiles folder on the local computer. Note that the schedule type option is specified as /sc weekly with the modifier /d mon,fri, which means every Wednesday and Friday.The /mo 2 modifier sets the interval at every two weeks.
Schtasks /create /tn 2WeekTask /tr C:\MyFiles\Mybatch.bat /sc weekly /mo 2 /d mon,fri
Scheduling a Task That Runs on the First Day of Every Month The following command creates a task named “First of Month” and runs it on the first day of every month on the local computer.The task runs a batch file named Mybatch.bat from the C:\MyFiles folder on the local computer. Note that the schedule type option is specified as /sc monthly. Since the /mo modifier is not used, the task defaults to the first day of every month.
Schtasks /create /tn "First of Month" /tr C:\MyFiles\Mybatch.bat /sc monthly
Scheduling a Task That Runs on the Fifteenth of Every Month
The following command creates a task named “Day 15 Every Month” and runs it on the fifteenth of every month on the local computer.The task runs a batch file named Mybatch.bat from the C:\MyFiles folder on the local computer. Note that the schedule type option is specified as /sc monthly. Since the /mo modifier is not used, the default interval of one month is used.The modifier /d 15 sets the task to run on the fifteenth of every month.
Schtasks /create /tn "Day 15 Every Month" /tr C:\MyFiles\Mybatch.bat /sc monthly /d 15
Scheduling a Task That Runs on the Last Day of Every Month
The following command creates a task named “Last Day of Month” and runs it on the last day of every month on the local computer.The task runs a batch file named Mybatch.bat from the C:\MyFiles folder on the local computer. Note that the schedule type option is Managing Scheduled Tasks • Chapter 3 83
specified as /sc monthly.The /mo lastday modifier sets the interval to the last day of every month.
Schtasks /create /tn "Last Day of Month" /tr C:\MyFiles\Mybatch.bat /sc monthly /mo lastday
Scheduling a Task That Runs on the Second and Fourth Friday of Every Month The following command creates a task named “Two Fridays” and runs it on the second and fourth Friday of every month on the local computer.The task runs a batch file named Mybatch.bat from the C:\MyFiles folder on the local computer. Note that the schedule type
option is specified as /sc monthly.The /mo second,fourth modifier sets the interval to the second and fourth and the /d fri parameter sets the day to Friday.
Schtasks /create /tn "Two Fridays" /tr C:\MyFiles\Mybatch.bat /sc monthly /mo second,fourth /d fri
In the preceding command, the weeks of the month have been specified as second and
fourth with the /mo parameter.Valid options for this modifier are first, second, third, fourth,
and last.
Scheduling a Task That Runs on the Second Friday in March, June, September, and December The following command creates a task named “Selected Days and Months” and runs it on the second Friday of the months of March, June, September, and December on the local computer. The task runs a batch file named Mybatch.bat from the C:\MyFiles folder on the local computer. Note that the schedule type option is specified as /sc monthly.The /mo second modifier sets the interval to the second and the /d fri parameter sets the day to Friday.The /m mar,jun,sep,dec parameter sets the months to March, June, September, and December, respectively.
Schtasks /create /tn "Selected Days and Months" /tr C:\MyFiles\Mybatch.bat /sc monthly /mo second /d fri /m mar,jun,sep,dec
Scheduling a Task That Runs on a Remote Computer with a User Account
The following command creates a task named “Remote Task” to run every 30 minutes on a remote computer named HR File Server01.The task is run with the username garyb from the Syngress domain.The task starts at 8:00 every night and ends the next day at 7:00 A.M. This command is useful for scheduling a task that should start after office hours and stop before the office reopens.The /st and /se parameters have been used for start and end time, respectively. You can also use the /k parameter to stop the task at 7:00 A.M. if it is still running. 84 Chapter 3 • Managing Scheduled Tasks
Schtasks /create /tn "Remote Task" /s "HR File Server01" /u Syngress\Garyb /tr C:\MyFiles\Mybatch.bat /sc minute /mo 30 /st 30:00 /et 07:00