Programming Field

Schtasks /Query - DOS/Command Prompt Reference

Displays the registered tasks. This is one of the commands (options) of Schtasks.

Syntax

schtasks[.exe] /Query [/S <remote-system> [/U <user-name> [/P [<password>]]]]
    [/FO <output-format> [/NH] [/V] | /XML [ONE]]
    [/TN <task-name>] [/HRESULT]

Options

* If you use ‘/XML’, ‘/FO’, ‘/NH’, and ‘/V’ cannot be specified.

/S <remote-system> Specifies the name of the computer (local/remote) where the task is 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).

/FO <output-format>

Specifies the format for outputting to the screen. The three available values are as follows. If /FO is omitted, it defaults to ‘TABLE’.

Value for formatMeaning
TABLEOutputs in a pseudo-table format. You can see multiple tasks lined up, but if ‘/V’ is specified, it may use a wider width, causing display issues in the default command prompt width.
LISTDisplays information for each task in a bullet-point-like format. It improves readability, but if /TN is not specified, the output volume may increase. Note that when specifying LIST, you cannot use the ‘/NH’ option.
CSVOutputs in CSV format. This is useful if you want to parse the output, such as passing it to a For command.
/NH Does not output headers when displaying in TABLE or CSV format.
/V Detailed output is performed. You can check the details of the task, but if you don't specify /TN, the output can be very extensive.
/XML [ONE]

Task information is output in XML format. If /TN is not specified, multiple ‘<Task>’ elements will be output under ‘<Tasks>’ as the root. Specifying ‘/XML ONE’ will suppress the XML declaration for each task, resulting in a standalone XML format.

When specifying /TN, the output will be an XML with ‘<Task>’ as the root. However, note that the format of the output remains the same regardless of whether ‘/XML’ or ‘/XML ONE’ is specified.

/TN <task-name>

Specifies the task name. If the task name contains space characters, you need to enclose it in double quotation marks (" "). Also, the path must be included if the task exists within a folder hierarchy. (You can prefix the whole task name with ‘\’, but it doesn't make a difference if you omit it.)

Note that you cannot specify ‘*’ for <task-name>. If you want to output all tasks, you can omit the ‘/TN’ parameter.

/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

When you use ‘schtasks /query’, it outputs either the specified task when using ‘/TN’ or all tasks if not specified. There's no way to output only specific tasks. Be aware that when outputting all tasks, it includes tasks registered by the system, which can result in a large amount of output.

The output generated by specifying both ‘/TN’ and ‘/XML’ options provides a format that can be directly used as the ‘/XML’ parameter in the ‘schtasks /create’ command. This makes it useful for backing up task information when needed.

Samples

Sample 1

schtasks /query

Outputs the names, next run times, and states of all tasks.

Sample 2 (Batch file)

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1 delims=," %%A in ('schtasks /query /fo csv /nh') do (
    set "FILE_NAME=%%~A"
    set "FILE_NAME=!FILE_NAME:\=_!
    schtasks /query /tn %%A /xml > "W:\TaskBackup\!FILE_NAME!.xml"
)

Exports each currently registered task to individual XML files in the ‘W:\TaskBackup’ directory. Parsing the output of ‘schtasks /query /fo csv /nh’ with a For command to extract the task names likely to appear at the beginning of the CSV, then passing them to ‘schtasks /query’ to obtain XML data.

* If you want to backup tasks, a simple method is to copy the files located in C:\Windows\System32\Tasks directory. The code above is just a sample demonstrating the use of ‘schtasks /query’.
* If your task names contain commas (which can be used in task names), the code provided above won't work as expected.

See also