3

I have two triggers in a task. The first one runs at a specific time. The second one starts at logon and runs every 10 minutes. I have many similar tasks like this situation. I want to use powershell to change the property from 10 minutes to 5 minutes and run indefinitely after logon. How do I specify the SECOND trigger?

$Task = Get-ScheduledTask -TaskName "Task"

$Task.Triggers.LogonTriggers.Repetition.Duration = "" $Task.Triggers.Repetition.Interval = "PT10M"

1 Answer 1

2

You can modify the $Task object and pass it into the Set-ScheduledTask which will apply the changes you've made. The first trigger that runs at a specific time will have it's StartBoundary property set, the second trigger which starts at Logon won't have this property set so we'll use the value of that to make sure we change the correct trigger.

$Task = Get-ScheduledTask -TaskName "Task"
$RepeatingTrigger = $Task.Triggers | Where-Object { $_.StartBoundary -eq $null }
$RepeatingTrigger.Repetition.Interval = "PT5M"
Set-ScheduledTask -InputObject $Task
Sign up to request clarification or add additional context in comments.

8 Comments

Thankyou for your help. May I ask if THIS trigger is instead the FIRST one, how do I put it?
I’ve selected the trigger to change based on it’s properties, that way you don’t need to worry about whether it was the 1st or 2nd trigger to be added. The approach in the answer will always get the trigger that doesn’t have a StartBoundary, ie the trigger that starts at LogOn and not the trigger that starts at a set time. If you wanted to always change the first trigger you could do something like $RepeatingTrigger = $Task.Triggers[0], but if your going to be performing this change on lots of scheduled tasks and the triggers haven’t always been added in the same order that won’t work
If your existing task runs indefinitely then the commands in the answer won't change that, if you want to make sure set the EndBoundary property to $null. You can drill down into the properties of $Task to see what is used to set the trigger type, it looks like it might be a property called StateChange but I think you'll need to call .NET methods directly to set that, for more info on getting started with that see here - docs.microsoft.com/en-us/powershell/module/… , it's quite a bit outside the scope of your question.
That cmdlet is for a scheduled job not a scheduled task, they’re not the same thing. Your question asked about a scheduled task. If you run the commands in my answer but change “PT5M” to “PT3M” the task will repeat every 3 minutes
@zexal985236 if the answer has solved the question you originally posed you should click the grey checkmark to accept it
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.