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.

4 Answers

I'm using Windows 10 Pro and PowerShell 7 and the following works well for me.

Creating a new task:

# Creating a task with multiple triggers and different execution limits $taskName = "MyTask" $trigger1 = New-ScheduledTaskTrigger -Once -At "2021-05-10 12:00:00" $trigger1.ExecutionTimeLimit = "PT20M" $trigger2 = New-ScheduledTaskTrigger -Once -At "2021-05-13 17:30:30" $trigger2.ExecutionTimeLimit = "PT50M" $taskTriggers = @( $trigger1, $trigger2 ) $taskAction = New-ScheduledTaskAction -Execute "notepad.exe" Register-ScheduledTask -TaskName $taskName -Trigger $taskTriggers -Action $taskAction 

For updating the trigger of an existing task the solution proposed by @TobyU worked well for me:

# Updating execution limits of a task that already exists $taskName = "MyTask" $task = Get-ScheduledTask -TaskName $taskName $task.Settings.ExecutionTimeLimit = "PT30S" # Global limit $task.Triggers[0].ExecutionTimeLimit = "PT10S" # Limit for trigger 1 $task.Triggers[1].ExecutionTimeLimit = "PT15S" # Limit for trigger 2 Set-ScheduledTask $task 

However, you can also completely replace the old trigger with a new one:

# Completely replacing all triggers with new one of an already existing task $taskName = "MyTask" $trigger = New-ScheduledTaskTrigger -Once -At "2021-05-17 17:17:17" $trigger.ExecutionTimeLimit = "PT42M" Set-ScheduledTask -TaskName $taskName -Trigger $trigger 

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.

5

Create Schedule Task Remotely

Invoke-Command -ComputerName Computername -Scriptblock { $action = New-ScheduledTaskAction -Execute 'C:\app.exe” ' $trigger = New-ScheduledTaskTrigger -Daily -At 10am -RandomDelay (New-TimeSpan - Minutes 480) $principal = New-ScheduledTaskPrincipal -GroupID "BUILTIN\Administrators" - RunLevel Highest Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal - TaskName "Schedule_task_name" -Description "Task Description" $Task = Get-ScheduledTask -TaskName "Schedule_task_name" $Task.Triggers[0].ExecutionTimeLimit= "PT30M" Set-ScheduledTask $Task } 

Looks like your format for the ExecutionTimeLimit may need tweaking. In my case, I wanted the setting removed (ie the tickbox unticked). I couldn't figure this out, but using the following format for ExecutionTimeLimit did work, so I could set it to a far away value: d.hh:mm:ss, ie 1000 days:

$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit 1000.00:00:00 Register-ScheduledTask -TaskName 'name' -Action $action -Trigger $trigger -Settings $settings 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.