PowerShell – Creating Scheduled Tasks with PowerShell version 3

I am currently working on a script where I need to create a scheduled task that runs a powershell script at a given time. With PowerShell 4.0 a scheduled task can be easily created with the new cmdlets New-ScheduledTaskAction, New-ScheduledTaskTrigger and Register-ScheduledTask, but unfortunately i have to create a solution that works on clients running PowerSshell version 3.0.

When searching the web for examples how others have solved this I found many examples where people invoke the schtasks.exe command but in order to further improve my PowerShell skills I wanted to avoid calling external executables and do as much in PowerShell as possible. So after a bit of further searching I found an interesting post from @letitknowblog who uses the Task Scheduler’s com object for creating a scheduled task.

To simplify sharing my lessons learned I’ve created a simple script that does the following.

  • Define variables for the Schedled Task name, description, command,  command argument to be executed
  • Define the time when the scheduled task must run, the below task will only run once.
  • Create the scheduled task, actions and triggers

Now if we wanted to run the Task more than just once, let’s say on a monthly basis, we have to change and add a bit of code  The code above uses Create(1) which means that the trigger is set to run once.

If we want to use another schedule we must use one of the following values as explained in more detail here.


So let’s suppose we want to run the task the first day of every month, we then have to change the code as following.

If we want to run the task when the system is idle we set the value of Create to 6.

If more than one action is required, just add an action, action path and action argument as in the example below.

The following code registers the scheduled task

If the first parameter is set to $null, a random GUID is assigned as the Task name.

The $Taskdefinition holds all the previously defined settings for the scheduled task.

The value 6 referts to the task creation constant which means that Task Scheduler either registers the task as a new task or as an updated version if the task already exists. A complete listing of possible values can be found here.

The next value defines the user context in wich the task runs, in this case we run the scheduled task within the system context.

The last value defines the logon type. The value of 5 Indicates that a Local System, Local Service, or Network Service account is being used as a security context to run the task. A complete listing of possible values can be found here

Much simpler when using PowerShell version 4

With PowerShell version 4 things get a bit simpler, to accomplish the same as in the aboe scrpt only 3 lines of code are needed.

3 thoughts on “PowerShell – Creating Scheduled Tasks with PowerShell version 3

  1. Alex,

    Actually, PowerShell 3 on Windows Server 2012 includes the task scheduler cmdlets (ScheduledTasks module). Sadly, Microsoft chose not to back-port this module to Windows 7/Server 2008 R2.

    You didn’t mention what OS you were working from.

  2. Hi,
    Thank you very much for this script and all the information that you provide in this article.
    It was very helpful for me.
    I am trying to add to this script a trigger that will make the task repeat itself every 5 minutes and have no luck.
    Can you please help me to achieve this?
    Thank you,

Leave a Reply

Your email address will not be published. Required fields are marked *