Infinite time in PowerShell?
Asked Answered
D

2

6

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

Dhahran answered 29/4, 2015 at 20:28 Comment(2)
This is interesting. Usually when creating a scheduled task in the GUI, I would make it Daily, repeating every 5 minutes for a duration of 24 hours. But that combination is not possible with this cmdlet.Praetor
Here's infinity: 1.0/0 aww it has to be integerRefulgent
S
10

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

Spancake answered 29/4, 2015 at 20:40 Comment(6)
It's not quite infinite, but I'd say 30,000 years is about sufficent. Definitely A lot better than just ~27 years. If someone figures out how to make it indefinite, I'll revise the answer chosen. Thanks! EDIT It turns out that windows interprets this as indefinite in the GUI. i.imgur.com/n1NZ3Qg.pngDhahran
200+ years is “infinite”.Kurth
Strangely enough, this actually seems to configure the schedule task correctly. If you export it to XML, you get the <StopAtDurationEnd>false</StopAtDurationEnd> element. It must be some kind of undocumented sentinel value.Ardie
What do I do after 30,000 years?Tour
This does not work anymore on recent systems. You now must remove the -RepetitionDuration option and your task will be re-executed by default infinite times.Landloper
As previously mentioned, this no longer works. The maximum allowed value is now 31 days (learn.microsoft.com/en-us/windows/win32/taskschd/…).Osteotomy
V
3

Omit RepetitionDuration:

$trigger = New-ScheduledTaskTrigger -Once -At '12:00am' -RepetitionInterval ([TimeSpan]::FromHours(2))

This runs at 12 AM and every 2 hours thereafter indefinitely.

Notes

This is per @FireEmerald's comment on the currently accepted answer, which doesn't work in PowerShell 7.2.5 on Server 2016.

The answer recommends setting RepetitionDuration to [TimeSpan]::MaxValue, which now errors out:

Register-ScheduledTask: The task XML contains a value which is incorrectly formatted or out of range.
(8,42):Duration:P99999999DT23H59M59S
Vasculum answered 15/12, 2022 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.