r/PowerShell • u/LordLoss01 • May 19 '26
Question Create scheduled task to run at logon and repeat indefinitely every 3 minutes?
I have this:
$TaskName = "Automation"
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument '-ExecutionPolicy Bypass C:\Automation\Automation.ps1'
$Trigger = New-ScheduledTaskTrigger -AtLogOn
$Principal = New-ScheduledTaskPrincipal -GroupID "Builtin\USERS" -RunLevel Highest
$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
Register-ScheduledTask -TaskName $TaskName -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings -ForceHowever, I need it to run again every 3 minutes after login. I tried doing
$Trigger = New-ScheduledTaskTrigger -AtLogOn -RepeatIndefinitely -RepetitionInterval (New-TimeSpan -Minutes 3)
but that didn't work. Anyone have any suggestions?
9
Upvotes
9
u/purplemonkeymad May 19 '26
You can't use "-AtLogOn" with "-RepeatIndefinitely". You'll either need to create multiple triggers for each of the events you need, or you'll need to create the task's xml directly so you can combine the options.
If you create it once in the UI you can then pull the xml out of the task file in $Env:SystemRoot\system32\tasks.
3
u/jborean93 May 19 '26
The answer from /u/robwe2 is certainly simpler but if you wanted to build the repetition object yourself you can do so like this and then set that to the triggers
Repetitionproperty.1
u/robwe2 May 19 '26
I did this:
$trigger1 = new-scheduledtasktrigger -atlogon
$trigger2 = new-scheduledtasktrigger -once -at (get-date) -repetitioninterval (new-timespan -minutes 1)
$trigger1.repetition = $trigger2.repetition
Then use $trigger1 as trigger when creating a new task
1
u/turbokid May 23 '26
What task could possibly require rerunning every 3 minutes? That is almost 500 runs every day
1
u/EuphoricFly5489 May 24 '26
You need two: one for logon and one for every 3 minutes.
1
u/robwe2 May 19 '26
Can add complete code because I’m on mobile
0
u/PS_Alex May 19 '26
Why not just keep your
Automation.ps1script permanently running with an infinite loop?I.e. have your script be launched by a scheduled task that run at logon, and in your script itself:
0
u/dodexahedron May 20 '26
This sounds like a job for a user-scoped service, not a scheduled task.