Powershell: New-ScheduledTaskTrigger cmdlet startup and Repetition

Arnaud Bougeard 0 Reputation points
2024-01-03T15:28:50.15+00:00

Hello

I would like to create via a Powershell script a task trigger on the startup of the machine with a repetition every 10 minutes. I manage to create it via the task creation tool on MMC.
But in PowerShell, I get an error indicating a conflict.

Have you any idea ?

Windows for business Windows Server User experience PowerShell
Windows for business Windows Client for IT Pros User experience Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2024-01-03T17:42:28.2266667+00:00
    unregister-scheduledtask -TaskName "Test02" -confirm:$false -ErrorAction SilentlyContinue
    $ac = New-ScheduledTaskAction -Execute "cmd.exe"  -Argument "/c timeout.exe /t 5"
    $tr = New-ScheduledTaskTrigger -AtStartup  
    $pr = New-ScheduledTaskPrincipal  -Groupid  "system"
    # create a trigger that won't be used, we just want to copy the repetiton cycle
    $tr.Repetition = (New-ScheduledTaskTrigger -Once  -at "12am" -RepetitionInterval (New-TimeSpan -Minutes 10)  ).repetition
    Register-ScheduledTask -TaskName "Test02" -Trigger $tr -Action $ac   -Principal $pr
    
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2024-01-03T19:43:54.2766667+00:00

    To repeat a scheduled task with AtStartup as the trigger you can't use the PowerShell cmdlet. Either use the command line utility or COM:

    https://stackoverflow.com/questions/47611878/powershell-scheduled-task-at-startup-repeating

    Or, combine the Powershell cmdlet(s) with a CIM object: https://stackoverflow.com/questions/61626369/use-powershell-to-add-property-on-workstation-unlock-to-scheduled-task/61646465#61646465


  3. Shujun Chen (Shanghai Wicresoft Co Ltd) 5 Reputation points Microsoft External Staff
    2024-01-12T02:39:56.0033333+00:00

    Hello, Thank you for posting in Q&A forum. The trigger -Once could be conflicted with the -AtStartup and cause this issue, please try to set up the repetition cycle instead of creating a separate trigger. You can define the RepetitionInterval by following command: $tr = New-ScheduledTaskTrigger -AtStartup -RepetitionInterval ([TimeSpan]:FromMinutes(10)) -RepetitionDuration ([TimeSpan]:MaxValue) It means this task starts when system startup and repeats every 10 minutes. Make sure to run the script with sufficient privileges as creating scheduled tasks may require administrative permissions. New-ScheduledTaskTrigger REF: https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtasktrigger?view=windowsserver2022-ps Best Regards,
    Shujun

    ---If the Answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.