The PowerShell windows scheduled task cheat sheet
Windows task scheduler is a wonderful tool that enables you to schedule tasks in a variety of different ways:
- Only on weekends
- At specific hours of a day
- By the day of the month (every first day of the month), etc.
And enable the following features:
- Trigger in parallel multiple instances
- Stop instances that take more than 5 minutes.
- Trigger by specific user/System
- Trigger with highest privileges
- Multiple schedules type
Besides, there are a couple of options to investigate schedule tasks for bugs and behaviors:
- By getting a specific instance event log (using the command: Get-WinEvent, below is an example)
- 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
- Understand how many failures and number of missing running using the super simple PowerShell script:
The results look like this:
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:
- the Execute arg refers only to cmd command and not to its args; “shutdown /s”: shutdown is the command, while /s is the argument.
- 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”.
- “-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
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:
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