How to add a cron job on Windows Scheduled tasks and cron jobs on Windows

On Microsoft Windows, cron jobs are known as Scheduled Tasks. They can be added through the Windows Task Scheduler user interface, by using PowerShell or with help of schtasks.exe.

Running a task at specific time or at recurring dates is one of the common administrative tasks on all operating systems. On Linux, the tool cron is used for this purpose. Microsoft Windows’ counterpart for cron is the Scheduled Task.

Each Scheduled Task is planned by the Task Scheduler. This application has been available since Windows NT 4.0, known as Task Scheduler 1.0. On modern Windows systems (Windows Server 2016, 2019, Windows 10 etc.), the Task Scheduler is available in version 2.0.

How to add a scheduled task

There are multiple way, how recurring tasks can be added on Windows.The following three methods require that you are logged in with an administrative account. Make sure that you have access to an account with administrative permissions!

Using the Task Scheduler

You can open the Windows Task Scheduler by either

  • clicking on Start and typing Task Scheduler
  • or hitting Win+R and typing taskschd.msc

If you don’t have administrative permission, you have to hit Win+R and type

runas /user:${ADMIN} taskschd.msc

${ADMIN} must be replaced with your administrator’s account username.

In both cases, the following Microsoft Management Console (MMC) will openTask Scheduler MMC

In the right screen click on Create Basic Task:

Create Basic Task
  • The now opening window is a wizard. You can specify when to execute a specific application.

On the first screen, type a name and description of your new cron job

  • Select *Trigger* on the second screen. A trigger is an event which starts a specific task.Task wizard trigger
  • You can choose between
    • Daily
    • Weekly
    • Monthly
    • One time
    • When the computer starts (before you have logged on)
    • When I log on
    • When a specific event is logged

  • On the Action tab you can specify which command has to be executed. Specify a path to the .exe or .bat file Task wizard action
  • On the last screen you see all information about the task. Click on Finish to create the task.

The new scheduled task is automatically activated. It will be started when the next specified trigger is hit.

Using the command line tool schtasks.exe

You can also add the new scheduled tasks by using the command line. This is handy if you already have a batch file to automate something. For more complex or dynamic task definitions, you might want to use the PowerShell method below.

On Windows, you can use schtasks.exe to create, delete or modify scheduled tasks. To use schtasks.exe, start a new command line as an administrator by hitting Win+R and type cmd.exe. Everything you can do with the Microsoft Management Console can be done with schtasks.exe.

schtasks.exe can be executed with a number of options. Please take a look at Microsofts official documentation for all available options. The most common used options are the following:

ArgumentDescription
/CreateAdd a new scheduled task
/tnName of task
/scschedule frequency (MINUTE, HOURLY, DAILY and so on)
/dOn which day or day of month the task shall be scheduled. You can use * for scheduling on every day
/stStart time in format HH:mm
/trApplication to run, e.g. cmd.exe

To add a new task, you can use the following code

schtasks.exe /create /tn my-task /sc daily  /st 13:00 /tr cmd.exe

This would execute cmd.exe every day on 13:00. The task automatically appears in the Task Scheduler MMC:

Task added with schtasks.exe

Using PowerShell

You can also add new cron jobs with help of PowerShell. This can be handy if you need to programatically add tasks on many Windows machines.

Start a new PowerShell by hitting Win+R and type powershell. You need the following three commands to add a new task:

$action = New-ScheduledTaskAction -Execute 'cmd.exe'
$trigger = New-ScheduledTaskTrigger -Daily -At 1pm
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "my-task"
Task added with PowerShell

Frequently Asked Questions

Where are Windows scheduled tasks stored?

This one might come in handy if you are debugging scheduled tasks or jobs. In some cases, your task is not executed due to some quoting or escaping issues.

Windows stores scheduled jobs and tasks in the following locations als XML files:

TypeLocationInfo
Task%WINDIR%\System32\TasksStored as XML file
Job%HOME_DIR%\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobsStored as XML file but not the same as the Task XML file definition

Does cron work on Windows?

If you are familiar with Linux, you can also make use of the Linux cron service inside the Windows Subsystem for Linux. One problem at the moment is, that cron only works during the lifetime of the WSL session. As soon as the WSL session is closed, cron will also be closed.

You should use one of methods above to add a cron job on Windows.

Are scheduled jobs and scheduled tasks in PowerShell are the same?

If you are creating new scheduled tasks with help of PowerShell you might find this interesting. In PowerShell exists scheduled tasks but also scheduled jobs. The New-ScheduledTask cmdlets are just a frontend for schtasks.exe and the Scheduled Task MMC.

Scheduled tasks have one big drawback: You are not able to capture the output of the executed command. With scheduled jobs you can grab the output. Please read this article if you need more details.

credit:https://docs.active-directory-wp.com/Usage/How_to_add_a_cron_job_on_Windows/Scheduled_tasks_and_cron_jobs_on_Windows/index.html