The PowerShell windows scheduled task cheat sheet

Eyal Segal
3 min readDec 22, 2020

Windows task scheduler is a wonderful tool that enables you to schedule tasks in a variety of different ways:

  1. Only on weekends
  2. At specific hours of a day
  3. By the day of the month (every first day of the month), etc.

And enable the following features:

  1. Trigger in parallel multiple instances
  2. Stop instances that take more than 5 minutes.
  3. Trigger by specific user/System
  4. Trigger with highest privileges
  5. Multiple schedules type

Besides, there are a couple of options to investigate schedule tasks for bugs and behaviors:

  1. By getting a specific instance event log (using the command: Get-WinEvent, below is an example)
  2. Understanding roughly the time of each instance:
    a. Using the event log history — if event 322 appears, it might indicate that each instance takes a long time
    b. See in the scheduled task history the start and end time of each task
  3. Understand how many failures and number of missing running using the super simple PowerShell script:
    Get-ScheduledTaskInfo “ScheduledTaskName”
    The results look like this:
Get-ScheduledTaskInfo
Get-ScheduledTaskInfo “ScheduledTaskName” example

How to get the history of a scheduled task?

Simply by running the following Powershell script:

After running it, you will get a list of chronological events with the following information: “Time created” (when the log was written), Id (event id), and the message.

These are the most popular event ids:

Things to notice when creating a scheduled task via Powershell?

First, read the “New-ScheduledTask” doc in Microsoft. then notice the following things:
Action : New-ScheduledTaskAction:

  1. the Execute arg refers only to cmd command and not to its args; “shutdown /s”: shutdown is the command, while /s is the argument.
  2. Aliases may work in one environment and not in another.
    As stupid it sounds, the “Powershell” command may work in one environment; others will require to use “powershell.exe” and another the full path: “C:\Windows\System32\WindowsPowerShell\v1.0\powershel.exe”.

params:

  • “-Execute”: A cmd command and its args (e.g., powershell.exe, and not powershell.exe ps_file.ps) You should notice that a different environment could have different aliases: if “powershell.exe” or even “Powershell” could be unfamiliar for some environments, and you may want to use the full path to make it more robust.
  • ”Argument”: a list of args separated by a space

trigger: New-ScheduledTaskTrigger

Notice that although the “straight forward” ways to trigger tasks are limited, you can always find a good workaround for a unique one.

For example, if you want to trigger a scheduled task every 30 minutes (which is not hourly and not every minute), you can use the args: RepetitionInterval and RepetitionDuration: New-ScheduledTaskTrigger -Once -RepetitionInterval (New-TimeSpan -Minutes 30) -RepetitionDuration (New-TimeSpan -Hours 23 -Minutes 55)

How to define the maximum time for your scheduled task:
You need to define ExecutionTimeLimit from the format: PTxx: “pt5M” is 5 minutes and MultipleInstances to be “Stop the existing instance.” Look at this example:

$currentTask = Get-ScheduledTask -TaskName ScheduledTaskName
$settings = New-ScheduledTaskSettingsSet
$settings.ExecutionTimeLimit = “PT25M”
$settings.CimInstanceProperties.Item(‘MultipleInstances’).Value = 3 # 3 corresponds to ‘Stop the existing instance’
Set-ScheduledTask -TaskName ScheduledTaskDisplayName -Trigger $currentTask.Triggers -Action $currentTask.Actions -Settings $settings

--

--