Scheduling a Task to Run Indefinitely

5,104

Solution 1

Instead of using -Once -At you could be using -AtStartup or -AtLogon

-AtStartup

$trigger = New-ScheduledTaskTrigger -AtStartup -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration ([timespan]::MaxValue)

-AtLogon

$trigger = New-ScheduledTaskTrigger -AtLogon -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration ([timespan]::MaxValue)

Solution 2

I use the following method to achieve the desired result:

$T = New-ScheduledTaskTrigger -AtStartup
$RT = New-ScheduledTaskTrigger -Once -At 7am -RepetitionDuration (New-TimeSpan -Days 1)  -RepetitionInterval  (New-TimeSpan -Minutes 1)
$T.Repetition = $RT.Repetition
Share:
5,104

Related videos on Youtube

Geoffrey McCosker
Author by

Geoffrey McCosker

Updated on September 18, 2022

Comments

  • Geoffrey McCosker
    Geoffrey McCosker over 1 year

    I suppose what I want is closer to a service, but I'd like to know if I could just accomplish this with a scheduled task.

    I can create a task with these parameters:

    $trigger = New-ScheduledTaskTrigger -Once -At 7am -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration ([timespan]::MaxValue)
    

    But if the computer boots up after 7 AM the task never fires right?

    How can I have a task that repeats every 5 mins no matter the time when the computer is on?

  • Geoffrey McCosker
    Geoffrey McCosker about 8 years
    Hmm I didn't think Atlogon supported repetitioninterval. When I run that command I get this error: New-ScheduledTaskTrigger : Parameter set cannot be resolved using the specified named parameters.
  • Bader H.
    Bader H. over 7 years
    -1. AtStartup does not support RepetitionInterval. @red888 what did you end up using as a solution?