search
top

Creating and Managing Scheduled Tasks from the Command Line in Windows

Introduction

Scheduled tasks are a wonderful tool to use in Windows to perform repetitious tasks on the server and your system. This can be updating, backups, cleanups and many other tasks. There are many different ways using command line and the GUI to accomplish adding, changing and deleting tasks. In this post we will cover using the command line to manage and maintain scheduled tasks. Scheduled tasks are kept in the C:\Windows\System32\Tasks directory and are xml files that contain the job information.

Managing Tasks with schtasks Command

Now we are ready to dig in and start managing our tasks from the command line. Microsoft has provided a way to do this with the schtasks command.

Step 1 – How To Create a Scheduled Task

If you are logged into the same computer where you want to run the scheduled task, then you can use the below command to create the task.

schtasks create /RU username /RP password /SC schedule_frequency /MO Schedule_modifier /D days /M months /TN taskname /TR Task_command /ST start_time /SD start_day /ED end_date

Now let’s see few examples.

Example 1: Schedule disk defragmentation on every Saturday at 6AM. User credentials are administrator/password.

C:\> schtasks /create /RU administrator /RP password /SC weekly /D SAT /TN defrag /TR c:\windows\system32\defrag.exe /ST 06:00:00

If the specified username and password are correct, then you would get the below message when you run the above command.

SUCCESS: The scheduled task "defrag" has successfully been created.

If the credentials are not correct, you may get a warning like below.

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

If there exists a scheduled task with the same name then the error would be:

specified task name already exists in the system.

If you need to use a domain user account to run the task you can specify domainname\username with /RU option.

You can also create a scheduled task to run as system with the following syntax.

C:\>schtasks /Create /SC DAILY /RU "SYSTEM" /TN DeleteLogs /ST 04:15 /TR c:\scripts\DeleteLogs.cmd

Here we are creating a daily task run a script at 4:15 am running as SYSTEM.

Step 2 – How to get the list of scheduled tasks

Just run Schtasks command and you can see the list of scheduled commands.

C:\>schtasks

TaskName Next Run Time Status
==================================== ======================== ===============
defrag 10:00:00, 3/12/2011
GoogleUpdateTaskUserS-1-5-21-3567637 11:14:00, 3/6/2011
GoogleUpdateTaskUserS-1-5-21-3567637 13:14:00, 3/5/2011

If you want complete details about each of the tasks you can run the command ‘Schtasks /query /v

Step 3 – Delete a scheduled task

We can delete a schedule task using ‘schtasks /delete /TN task_name‘ command. For example, to delete the task we created in the example 1 we can run the below command.

C:\>schtasks /delete /TN defrag

Delete all the scheduled tasks

You can run the below command to delete all the scheduled tasks.

C:\>schtasks /delete /TN *

Step 4 – Disable a scheduled task

There does not seem to be a way to disable a scheduled task from command line. We can delete the tasks as mentioned above.

Step 5 – Modify a scheduled task

We can change a scheduled task using ‘schtasks /change’ command. Run ‘schtasks /change /?’ for the syntax.

Bonus – Create a Folder with Scheduled Tasks

Now for a bit of organization. Say you have many tasks and you would like to group them in folders.

C:\>schtasks /create /xml "MyTask.xml" /tn "Admin\MyTask"

This creates a new task folder called Admin and creates a new task MyTask under the new folder

If the task needs to get created under an existing folder, try

schtasks /create /xml "MyTask.xml" /tn "Existing Task Folder\My New Task"

This creates a new task My New Task under an existing task folder Existing Task Folder

Conclusion

As we can see the schtasks command allows us to manage Scheduled Tasks in Windows easily in creating, deleting, modifying and organizing tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

top