Windows – Which SCHTASKS parameter(s) control the Setting “Stop the task if it runs for hours”

scheduled-taskwindows

I have a problem with writing a script to automate the creation of Scheduled Tasks in Windows.

After much Googling, I am unable to deduce which parameters to the command-line utility SCHTASKS.EXE control the setting "Stop the task if it runs for hours."

Best Answer

I cannot tell you that it is possible to automate this with schtasks. However, it can be done in powershell.

Once you create your task with schtasks (which you can do in powershell), you can use code like this to update the execution time limit:

$task = Get-ScheduledTask "MyTaskName"
$task.Settings #Optional to see what is going on
$task.Settings.ExecutionTimeLimit = "PT0S"
$task.Settings #Optional to see how it changed
Set-ScheduledTask $task

The key is knowing the PT0S setting. P3D is the default 3 days. PT0S unchecks the option and allows the task to run forever. If you want a definitive answer on a specific duration, set the options the way you want them in a task using the UI, then export the task. If you look at the resulting XML file, you can see the setting for ExecutionTimeLimit.