SamCyanide
SamCyanide

Reputation: 333

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

Upvotes: 6

Views: 6855

Answers (2)

Eric Eskildsen
Eric Eskildsen

Reputation: 4759

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

Upvotes: 3

Tony Hinkle
Tony Hinkle

Reputation: 4742

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

Upvotes: 10

Related Questions