Programming Field

Schtasks /Create - DOS/Command Prompt Reference

Registers a scheduled task. This is one of the commands (options) of Schtasks.

Syntax

schtasks[.exe] /Create [/S <remote-system> [/U <user-name> [/P [<password>]]]]
    /TN <task-name> /TR <command>
    /SC <schedule> [/MO <parameter>] [/EC <channel>]
    [/D <day>] [/M <month>] [/I <idle-time>] [/ST <start-time>]
    [/RI <interval>] [{/ET <end-time> | /DU <duration>} [/K]]
    [/SD <start-date>] [/ED <end-date>] [/DELAY <delay-time>] [/IT | /NP]
    [/RU <user> [/RP <password>]] [/RL <level>]
    [/XML <xml-file>] [/V1] [/Z] [/F] [/HRESULT]

Options

/S <remote-system> Specifies the name of the computer (local/remote) where the task will be registered. <remote-system> will be an IP address or hostname. If omitted, the local computer is the target.
/U <user-name> [/P [<password>]]

Specifies the login name and password when providing a computer name. You can specify a domain name in <user-name> (in the form of ‘domain\username’). If omitted, the user who executed this command will be used.

On ‘/P [<password>]’, if the entire ‘/P [<password>]’ is omitted, no password is set. If only ‘/P’ is specified (and ‘<password>’ is omitted), prompting the user to enter the password will occur (the entered password will be displayed as ‘*’ in the prompt).

/TN <task-name>

Specifies the task name. You can use characters that are valid for filenames and ‘\’ in ‘<task-name>’ (please also see ‘The task names’).

* If you want to include space characters, you need to enclose them with ‘" "’.

/TR <command>

Specifies the command to execute. ‘<command>’ will be the executable filename followed by parameters.

When specifying parameters for the command, you need to enclose the entire command line with ‘" "’. However, if you want to include ‘"’ in the command line, you write it as ‘""’.

* The interpretation of ‘"’ is slightly special. When you write ‘""’, it means ‘include a " in the command line at that position’ + ‘start/end the " " enclosure’. For example, if you want to pass the command line ‘hoge "piyo piyo"’, you need to write it as ‘"hoge ""piyo piyo""’.

/SC <schedule>

Specifies the execution timing (schedule) of the task. ‘<schedule>’ will be one of the following values:

Value Meaning
MINUTE The program will be executed at specified intervals in minutes. If /MO is not specified, it will execute at 1-minute intervals.
HOURLY The program will be executed at specified intervals in hours. If /MO is not specified, it will execute at 1-hour intervals.
DAILY The program will be executed daily. If /MO is not specified, it will execute at 1-day intervals.
WEEKLY The program will be executed weekly. If /MO is not specified, it will execute once a week. If /D is not specified, it will execute on Monday.
MONTHLY The program will be executed monthly. If /MO is not specified, it will execute once a month. If this value is used, /M must be specified. Additionally, unless /MO is ‘LASTDAY’, /D must also be specified.
ONCE The program will be executed only once (if not specified otherwise). If ONCE is specified, /ST must be specified.
ONSTART The program will be executed when the computer starts up. Creating this task requires administrator privileges. If /RU or /NP is not specified, the task will not be executed because it does not meet the condition ‘Run only when user is logged on’.
ONLOGON The program will be executed when any user logs in to the computer. Creating this task requires administrator privileges. If /RU or /NP is not specified, the task will only be executed when the user who created it logs in.
ONIDLE The program will be executed when the computer is in an idle state. Use /I to specify the idle time until execution.
ONEVENT [Windows Vista or later] The program will be executed when an event (viewable in Event Viewer) occurs. You need to specify the event with /MO (in XPath format), and in some cases, specifying /EC may also be necessary.
/MO <parameter>

Specifies parameters corresponding to the schedule type (specified by /SC). Specifically, you will specify the followings:

Type of /SC Values to specify
MINUTE Specifies the time interval (in minutes) for executing the task, between 1 and 1439.
HOURLY Specifies the time interval (in hours) for executing the task, between 1 and 23.
DAILY Specifies the number of days for executing the task, between 1 and 365.
WEEKLY Specifies the number of weeks for executing the task, between 1 and 52.
MONTHLY Specifies the multiples of the target month, between 1 and 12 (if you specify 3, the task will be executed in March, June, September, and December). Additionally, you can specify the following values:
FIRST The task will be executed on the first specified day of every month. You need to specify the day of the week with /D.
SECOND The task will be executed on the second specified day of the week every month. You need to specify the day of the week with /D.
THIRD The task will be executed on the third specified day of the week every month. You need to specify the day of the week with /D.
FOURTH The task will be executed on the fourth specified day of the week every month. You need to specify the day of the week with /D.
LAST The task will be executed on the last specified day of the week every month. You need to specify the day of the week with /D.
LASTDAY The task will be executed on the last day of the target month. You need to specify the target month with /M.
ONCE /MO cannot be specified.
ONSTART /MO cannot be specified.
ONLOGON /MO cannot be specified.
ONIDLE /MO cannot be specified.
ONEVENT Specifies a string in XPath format. (e.g. ‘*[System[Provider[@Name='Microsoft-Windows-Winlogon'] and EventID=7001]]’)
/EC <channel> [Windows Vista or later] Specifies the channel name of the event when registering a schedule with ‘/SC ONEVENT’ (such as ‘System’, ‘Applications’, ‘Windows PowerShell’, etc., the full name displayed in the properties in Event Viewer).
/D <day> Specifies the day or day of the week for the task's execution timing. If ‘/SC WEEKLY’ or ‘/SC MONTHLY’ with /MO as FIRST, SECOND, THIRD, FOURTH, LAST is specified, ‘<day>’ will be the day of the week with three English letters (SUN, MON, TUE, WED, THU, FRI, SAT). If ‘/SC MONTHLY’ and /MO is not one of the aforementioned options, ‘<day>’ will be the day to execute. For ‘/SC WEEKLY’ only, you can specify multiple days separated by commas without spaces.
/M <month> Specifies the month for the task's execution timing. ‘<month>’ will be the month using three English letters (JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC, *). If you specify ‘*’, it will execute every month. You can only specify this when ‘/SC MONTHLY’ and /MO is FIRST, SECOND, THIRD, FOURTH, LAST, LASTDAY (required for LASTDAY) are specified. You can specify multiple months separated by commas without spaces.
/I <idle-time> Specifies the idle (waiting) time in minutes. When ‘/SC ONIDLE’ is used, the task will be executed if the system is idle for the specified amount of time.
/ST <start-time> Specifies the start time of the task. ‘<start-time>’ will be the format ‘hh:mm’ (two digits for hours + ‘:’ + two digits for minutes) in 24-hour format. If omitted, the current time (at the time of executing the command) will be used. If ‘/SC ONCE’ is used, specifying /ST is mandatory. It cannot be specified when ‘/SC’ is ONSTART, ONLOGON, ONIDLE, or ONEVENT.
/RI <interval>

Specifies the interval in minutes for repeating the task. You can specify this only when ‘/SC’ is DAILY, WEEKLY, MONTHLY, or ONCE (except ONCE, it will be repeated further for each schedule, for example, if MONTHLY triggers every month, it will be repeated at the specified interval with /RI). The task will be repeated until the time calculated by /ET or /DU. It cannot be specified when ‘/SC’ is ONSTART, ONLOGON, ONIDLE, or ONEVENT.

If /ET or /DU is specified and /RI is omitted, the task will be executed at intervals of 10 minutes.

/ET <end-time> Specifies the end time (expiration) of the task. ‘<end-time>’ will be in the format ‘hh:mm’ (two digits for hours + ‘:’ + two digits for minutes) in 24-hour format. The end time must be later than the time when the task is executed for the first time, calculated from /ST (for example: if /ST is 16:25 and /RI is 1, then /ET must be after 16:27). It cannot be specified when ‘/SC’ is ONSTART, ONLOGON, ONIDLE, or ONEVENT.
/DU <duration> Specifies the duration of the task repetition in minutes. This value must be greater than the value specified with /RI (or default to 10 if not specified). It cannot be specified when ‘/SC’ is ONSTART, ONLOGON, ONIDLE, or ONEVENT.
/K When the end time calculated by /ET or /DU is reached, if the task is still running, a termination signal will be sent. In this case, the task execution log will indicate that it was forcibly terminated. It cannot be specified when ‘/SC’ is ONSTART, ONLOGON, ONIDLE, or ONEVENT.
/SD <start-date> Specifies the date to start the task. ‘<start-date>’ will be the date in year-based date format according to the system settings. If omitted, the current date will be used. It cannot be specified when ‘/SC’ is ONSTART, ONLOGON, ONIDLE, or ONEVENT.
/ED <end-date> Specifies the end date (expiration) of the task to be repeated. ‘<end-date>’ will be the date in year-based date format according to the system settings. It cannot be specified when ‘/SC’ is ONCE, ONSTART, ONLOGON, ONIDLE, or ONEVENT.
/DELAY <delay-time> [Windows Vista or later] Specifies the delay time from when the trigger occurs to when the command is actually executed when ‘/SC’ is ONSTART, ONLOGON, or ONEVENT. ‘<delay-time>’ will be the delay time in the format ‘mmmm:ss’ (for example: specify ‘0000:02’ for 2 seconds).
/IT

The task will only be executed if the user specified with /RU (or the user executing the command if not specified) is logged in. By default, the task is created with the same state as if /IT were specified. However, when using the /XML option to load task data, specifying this option allows you to overwrite the contents of the file specified by /XML.

Note that if SYSTEM is specified in /RU, /IT will be ignored.

/NP

The task will be executed even if the user specified with /RU (or the user executing the command if not specified) is not logged in. In this case, the password for the user is not saved. This option can only be specified when creating a local task. If a user other than the one who executed the Schtasks command is specified with /RU and /NP is used, access may be denied. Additionally, if /NP is used, the task will generally be executed as a background task, so programs that display user interfaces may behave unexpectedly.

Note that if SYSTEM is specified in /RU, /NP will be ignored.

/RU <user> Specifies the user to run the task. Built-in users such as ‘SYSTEM’ can also be used. If /NP is not specified, the task will only run if this user is logged in.
/RP [<password>] Specifies the password for the user specified with /RU. If ‘<password>’ is omitted (when only /RP is specified), you will be prompted for the password.
/RL <level> [Windows Vista or later] Specifies the level at which the task will run. ‘<level>’ must be either LIMITED (normal) or HIGHEST (highest privilege).
/XML <xml-file>

[Windows Vista or later] Specifies the XML file containing the information of the task you want to register. It should be written according to the ‘Task Scheduler Schema’. By using an XML file, you can apply items/settings to the task that cannot be specified with Schtasks options. If you create a task using /XML, all options except /TN become optional.

* Since there is no element in the XML file to specify the task name, /TN remains a mandatory option even when /XML is specified.

/V1 [Windows Vista or later] Create a task compatible with versions prior to Windows Vista (version 1.1). Some options may become unavailable.
/Z

Deletes the task if it is not executed further after its execution. (It will be deleted one second later in practice.)

* If /Z is specified, /DU cannot be used, and /ET must be specified. (If /DU is specified, an error will occur: ‘The task XML is missing a required element or attribute. (45,4):EndBoundary:’)

/F Forcefully overwrite and create the task even if it already exists.
/HRESULT [Windows 10? or later] Changes the exit code of the Schtasks command to HRESULT. This is used when you want to perform detailed handling in case of errors.

Details

You can register tasks using the ‘/Create’ option of Schtasks command. By registering tasks, you can schedule programs you want to start at startup or run periodically (similar to cron in Linux). However, please note that creating some task types such as "ONLOGON" requires administrator privileges.

* Registration for ‘task execution at the timing when a specific user logs in’ cannot be specified using parameters of the Schtasks command. You need to create an XML file in advance and specify it with the /XML parameter. (By creating and registering an XML file containing the settings for ‘execution at the timing when a specific user logs in’, you can register it without administrator privileges.)

Tasks created with Schtasks will have ‘Start the task only if the computer is on AC power’ enabled by default, except when configured in an XML file. Please note that tasks will not run if the computer is not connected to a power source, such as in the case of a notebook PC.

Tasks scheduled to run at a specified date and time will not run if the system is not powered on at that time.

Tasks are created as files under %windir%\System32\Tasks. These files are in XML format and can be specified using the "/XML" parameter of Schtasks. Additionally, by creating XML files in advance, you can register schedules based on them as part of the program installation process.

Samples

Sample 1

schtasks /create /tn CheckUpdate /tr D:\home\updater.exe /sc daily /st 17:00

Registers a task named ‘CheckUpdate’ to run ‘D:\home\updater.exe’ every day at 17:00. Since user options are not specified, the task will be executed with the current user's permissions only if the current user is logged in.

Sample 2

schtasks /create /tn CheckStatus /tr D:\home\checker.exe /sc monthly /mo first /m jan,mar,may,jul,sep,nov /d mon /st 10:00 /ru system

Registers a task named ‘CheckStatus’ to run ‘D:\home\checker.exe’ at 10:00 on the first Monday of January, March, May, July, September, and November. The task will be executed under the ‘SYSTEM’ account.

Sample 3 (Command Prompt)

for /f "tokens=2 delims=:" %A in ('whoami /user /fo csv /nh') do set "MY_SID=%~A"
schtasks /create /tn LaunchNotepad /tr notepad.exe /sc onevent /ec system ^
  /mo "*[System[EventID=7001] and EventData[Data[@Name='UserSid']='%MY_SID%']]" ^
  /delay 3

Registers a task named ‘LaunchNotepad’ to run ‘notepad.exe’ when the current user logs in. The first line executes the Whoami command to obtain the SID of the current user, and then assigns the result to the ‘MY_SID’ variable using For. The XPath expression ‘*[System[EventID=7001] and EventData[Data[@Name='UserSid']=' %MY_SID%']]’ means to match only if it is a Windows logon event (EventID: 7001) and the user is ‘%MY_SID%’. Additionally, a delay time of 3 seconds is specified to ensure proper execution of the program (3 seconds may not be sufficient).

Note that if you want to run a task when a specific user logs on, it's better to create an XML file like the following and load it into Schtasks, or configure it via the Task Scheduler GUI:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <Triggers>
    <LogonTrigger>
      <Enabled>true</Enabled>
      <UserId>Specify the username here</UserId>
    </LogonTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <RunLevel>LeastPrivilege</RunLevel>
      <UserId>Specify the username here</UserId>
      <LogonType>InteractiveToken</LogonType>
    </Principal>
  </Principals>
  <Actions Context="Author">
    <Exec>
      <Command>Enter the command you want to execute here</Command>
    </Exec>
  </Actions>
</Task>

If you save the above as Bar.xml (make sure to save it with BOM UTF-16 LE), you can register it with the command: schtasks /create /tn TaskName /xml Bar.xml

See also