Search code examples
powershellschedulerwindows-task-scheduler

how to configure "stop the task if it runs longer than" for schtasks.exe


I want to set the value for the option "Stop task if runs longer than" in the windows schedule task Trigger tab using Powershell.

Already tried with /DU switch but it is not working.

Below is the screenshot for the same. Windows Task Schedule Trigger Settings

let me know in case of any further information is required.

@TobyU: I tried your suggestion as well but it is not setting up the required value. Below is the screenshot for your reference. enter image description here

Thank you in advance.


Solution

  • You can set it for the whole task at once:

    $task = Get-ScheduledTask -TaskName "MyTask"
    $task.Settings.ExecutionTimeLimit = "PT3H"
    Set-ScheduledTask $task
    

    Stops after 3 hours in the above example.

    This is how you set it only for a specific trigger:

    $task = Get-ScheduledTask -TaskName "MyTask"
    $task.Triggers[0].ExecutionTimeLimit = "PT3H"
    Set-ScheduledTask $task
    

    Where Triggers[0] is the specific trigger you want to adjust since $task.Triggers returns an array with all the available trigger objects for the specific task.