How to schedule a task with PowerShell to run every hour, Monday to Friday between 8 AM to 6 PM

12,932

This is the approach documented by Microsoft to address this.

Note: You must use the setting options provided in the drop down boxes, even though the GUI appears to be freeform fields, they are not.

See:

Configuring Advanced Scheduled Task Parameters Using PowerShell

The Missing Parameters

Great, so now we have a script, a scheduled task that runs the script, and it will run every time the server or workstation is started up. But we have failed to meet one of the requirements; the task must also repeat every 10 minutes for 1 day. If you go back and attempt to add a repetition interval or repetition duration you will notice that neither are an option for the -AtStartup parameter:

Through the GUI the scheduled task parameters are available:

But they are not available via the PowerShell New-ScheduledTaskTrigger cmdlet if you choose the -AtStartup parameter. They are available via the -Once parameter but that would not meet the requirements. The solution is to directly modify the task's properties using its XML schema. This may sound complicated at first but in reality it is very easy and will allow you to modify practically every parameter of the scheduled task.

Step 1 - Get the scheduled task. The first step is to get the scheduled task that you just created and store it in an object.

$Task = Get-ScheduledTask -TaskName "SampleScheduledTask"

Step 2 - Update the specific property of the task that you wish to modify. For this example we wish to add a repetition duration and a repetition interval.

$Task.Triggers.Repetition.Duration = "P1D"
$Task.Triggers.Repetition.Interval = "PT10M"

You are probably wondering how I knew P1D would equal a duration of 1 Day and how I knew PT10M would equal a repetition interval of 10 minutes since neither are the typical date/time or timespan values that you are probably used to working with. To get those values I simply configured the duration and interval that I wanted through the GUI then viewed the result through Powershell using the following command:

$Task.Triggers.Repetition | fl *

Step 3 - Save the new configuration to the scheduled task. Remember, you must re-enter the credentials for the task here or the operation will fail.

$Task | Set-ScheduledTask -User "NT AUTHORITY\SYSTEM"
Share:
12,932

Related videos on Youtube

DivZ
Author by

DivZ

Updated on September 18, 2022

Comments

  • DivZ
    DivZ over 1 year

    I need to schedule a task to run:

    • Monday to Friday
    • Every hour from 8 AM to 6 PM

    I can do this from the regular task scheduler GUI but since I'm using a group managed service account to run this task, I need to do this from powershell.

    I've tried using the -RepetitionDuration and -RepetitionInterval parameters in the New-ScheduledTaskTrigger cmdlet but in order to use that, I need to have the -once parameter?

    Please advise!

    enter image description here

    • Ravindra Bawane
      Ravindra Bawane over 5 years
      What version of PowerShell? Have you looked into importing a scheduled task .xml file that already has the scheduled determined?
  • scrthq
    scrthq over 4 years
    Regarding the "odd" formatting of the Duration/Interval values, those are ISO 8601 durations. You can read more about them here: en.wikipedia.org/wiki/ISO_8601#Durations