How To Schedule Tasks From Command Line In Windows With Schtasks? – POFTUT

How To Schedule Tasks From Command Line In Windows With Schtasks?


Recurring tasks are generally schedules with scheduled task manager. Scheduled task manager have GUI for management but creating a task in 20 different servers is very hard and trivial job. Windows operating systems provide tool called schtasks which is used to create, modify, delete scheduled tasks.

Help

Help  about scheduling tasks command schtasks can be printed with /? option.

$ schtasks /?
Help
Help

Syntax

While using command we will use following syntax.

SCHTASKS /parameter [arguments]

Create A Scheduled Task/Job

In order to create a scheduled task we need to provide following parameters at least.

  • /SC specifies
  • /TN specifies task name which will identify task from other tasks
  • /TR specifies the tasks executable file or command.

In this example we will create a task named defrag which will run c:\windows\system32\defrag.exe at weekly periods.

$ Schtasks /create  /SC weekly  /TN defrag /TR c:\windows\system32\defrag.exe
Create A Scheduled Task/Job
Create A Scheduled Task/Job

Schtasks Attributes

While creating scheduled jobs we can providing following attributes.

  • /S system Specifies the remote system to connect to.
  • /U username Specifies the user context under which schtasks.exe
    should execute.
  • /P [password] Specifies the password for the given user context.
    Prompts for input if omitted.
  • /TN taskname Specifies which scheduled task to change.
  • /RU username Changes the user name (user context) under which the
    scheduled task has to run. For the system account,
    valid values are “”, “NT AUTHORITY\SYSTEM” or “SYSTEM”.
    For v2 tasks, “NT AUTHORITY\LOCALSERVICE” and
    “NT AUTHORITY\NETWORKSERVICE” are also available as well
    as the well known SIDs for all three.
    /RP password Specifies a new password for the existing user
    context or the password for a new user account.
    This password is ignored for the system account.
  • /TR taskrun Specifies the new program that the
    scheduled task will run.
  • /ST starttime Specifies the start time to run the task. The time
    format is HH:mm (24 hour time) for example, 14:30
    for 2:30 PM.
  • /RI interval Specifies the repetition interval in
    minutes. Valid range: 1 – 599940 minutes.
  • /ET endtime Specifies the end time to run the task. The time
    format is HH:mm (24 hour time) for example, 14:50
    for 2:50 PM.
  • /DU duration Specifies the duration to run the task. The time
    format is HH:mm. This is not applicable with /ET.
  • /K Terminates the task at the endtime or duration time.
  • /SD startdate Specifies the first date on which the task runs.
    The format is mm/dd/yyyy.
  • /ED enddate Specifies the last date when the task should run.
    The format is mm/dd/yyyy.
  • /IT Enables the task to run interactively only if the
    /RU user is currently logged on at the time the job
    runs. This task runs only if the user is logged in.
  • /RL level Sets the Run Level for the job. Valid values are
    LIMITED and HIGHEST. The default is to not change it.
  • /ENABLE Enables the scheduled task.
  • /DISABLE Disables the scheduled task.
  • /Z Marks the task for deletion after its final run.
  • /DELAY delaytime Specifies the wait time to delay the running of the
    task after the trigger is fired. The time format is
    mmmm:ss. This option is only valid for schedule types
    ONSTART, ONLOGON, ONEVENT.
LEARN MORE  Schedule Jobs and Tasks With Windows At Command

Create A Scheduled Tasks/Job With More Options

In previous example we have used to very little options. We can provide more needed attributes. In this example we will provide

  • /RU for the username
  • /RP for password
  • /SC for period
  • /D‘ for date
  • /TN for task name
  • /TR for executable file or command
  • /ST for time
$ Schtasks /create /RU jack /RP 123456 /SC weekly /D SAT /TN defrag /TR c:\windows\system32\defrag.exe /ST 10:00:00
Create A Scheduled Tasks/Job With More Options
Create A Scheduled Tasks/Job With More Options

List All Ready Created Scheduled Tasks/Jobs

In a IT environment there will be a lot of existing scheduled jobs. We can list these existing tasks just issuing schtasks like below.

$ schtasks
List All Ready Created Scheduled Tasks/Jobs
List All Ready Created Scheduled Tasks/Jobs

For each task following information is printed.

  • Folder shows namespace based location.
  • Taskname shows given name for the task to identify
  • Next Run Time shows when will the task will run for the next time
  • Status shows whether the task is ready to run or running or there is a problem.

Delete Scheduled Task/Job

All ready scheduled jobs can be deleted if we do not need it. We can delete task with /delete option and specifying the task name. In this example we will delete task named degfrag which is specified with /TN option.

$ schtasks /delete /TN defrag
Delete Scheduled Task/Job
Delete Scheduled Task/Job

While deleting we need to confirm the delete operation. If the task is deleted properly we will get a SUCCESS message.

Delete All Scheduled Task/Jobs

If we do not need all of the scheduled jobs and we want to delete them in a single shot we can use asterisks * sign to specify task name.

$ schtasks /delete /TN *

Disable Scheduled Task/Job

Some time we may need to disable scheduledjob for a short period. We can use /change option with /disable option in order to disable a scheduled job. In this example we will disable the job named defrag .

$ schtasks /change /tn defrag /disable
Disable Scheduled Task/Job
Disable Scheduled Task/Job

Enable Scheduled Task/Job

We can enable scheduled job similar to disable operation where we use /enable .

$ schtasks /change /tn defrag /enable
Enable Scheduled Task/Job
Enable Scheduled Task/Job

Modify Scheduled Task/Job

A scheduled task can be changed with /change option. We can specify the attribute we want to change. In this example we will change the username and password of the user which will run task with /RU and /RP .

$ schtasks /change /tn defrag /ru jack /rp 123456
Modify Scheduled Task/Job
Modify Scheduled Task/Job

Leave a Comment