Tuesday, November 19, 2013

Using command line to control Windows Scheduled Tasks

Manage Scheduled Tasks with the SchTasks.exe Command


An administrator can use the SchTasks.exe command-line utility to create, delete, query, change, run, and end scheduled tasks on a local or remote system through the command shell. The SchTasks.exe command interface uses the following syntax:
schtasks /{parameter} [arguments]


The Command Parameters Are:

  • /Create Creates a new scheduled task
  • /DeleteDeletes the scheduled task(s)
  • /Query Displays all scheduled tasks
  • /Change Changes the properties of scheduled task
  • /Run Runs the scheduled task immediately
  • /End Stops the currently running scheduled task
  • /? Displays this help message

You can control, monitor, change, etc Windows Scheduled Tasks using schtasks from the command line. You can use it on your local machine or a remote machine such as a server.
In the examples below let’s assume that the Windows Scheduled Task you are interested in is called EventLogImport.

IMPORTANT: For any of the commands or examples below, you can add a /S and then the servername to execute the command against a remote machine running Windows.

Monitoring

Basic Info on local machine

schtasks /QUERY /TN EventLogImport

Basic Info for remote machine

schtasks /QUERY /S serverNameHere /TN EventLogImport

Verbose Info

schtasks /QUERY /V /TN EventLogImport

Verbose Info with CSV output to screen

schtasks /QUERY /FO CSV /V /TN EventLogImport
NOTE: Other options are TABLE and LIST. The /V is for Verbose.

To remove the header row you can use the MORE command to start reading at the second line
schtasks /QUERY /FO CSV /V /TN EventLogImport | more +1

Verbose Info with CSV output to file

This will clear the status.csv if it exists, else it will create a new file.
schtasks /QUERY /FO CSV /V /TN EventLogImport > c:\temp\status.csv

To append to the file if you call two or more of these in a row, use the >> instead of >.
schtasks /QUERY /FO CSV /V /TN EventLogImport >> c:\temp\status.csv

To remove the header row you can use the MORE command to start reading at the second line
schtasks /QUERY /FO CSV /V /TN EventLogImport | more +1 >> EventLogImportScheduledJobStatus.csv

Controlling

Disable a Scheduled Task

schtasks /CHANGE /TN EventLogImport /DISABLE

Enable a Scheduled Task

schtasks /CHANGE /TN EventLogImport /ENABLE

Other Commands

You can also create, delete, run, or kill scheduled tasks as well. For information on how to do this, I recommend typing schtasks /? at any command prompt.
For help on a specific switch, try one of the following:

SCHTASKS
SCHTASKS /?
SCHTASKS /Run /?
SCHTASKS /End /?
SCHTASKS /Create /?
SCHTASKS /Delete /?
SCHTASKS /Query  /?
SCHTASKS /Change /?
SCHTASKS /ShowSid /?


No comments:

Post a Comment