Search code examples
powershellscheduled-taskswindows-server-2012timespan

Infinite time in PowerShell?


I'm trying to create a task which will repeat every 5 minutes indefinitely through powershell. However, I cannot figure out a way to do this through all my searching. New-TimeSpan -Days 9999 appears to be the maximum value, and no matter what I do I cannot get the time to go over 9999 days. Here's the trigger: $trigger = New-ScheduledTaskTrigger -Once -At $date -RepetitionDuration (New-TimeSpan -Days 9999) -RepetitionInterval (New-TimeSpan -Minutes 5)

$PSVersionTable.PSVersion reports what I assume to be v4, here's the output: Major Minor Build Revision 4 0 -1 -1


Solution

  • Use -RepetitionDuration ([timespan]::MaxValue)

    As of today, this gives you 10,675,199 days (almost 30,000 years).

    See https://superuser.com/questions/403595/creating-a-scheduled-task-in-windows-that-will-run-at-intervals-indefinitely