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.

TASK_TRIGGER_EVENT  0
TASK_TRIGGER_TIME 1
TASK_TRIGGER_DAILY  2
TASK_TRIGGER_WEEKLY 3
TASK_TRIGGER_MONTHLY  4
TASK_TRIGGER_MONTHLYDOW 5
TASK_TRIGGER_IDLE 6
TASK_TRIGGER_REGISTRATION 7
TASK_TRIGGER_BOOT 8
TASK_TRIGGER_LOGON  9
TASK_TRIGGER_SESSION_STATE_CHANGE 11

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.

2 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.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Spam Protection by WP-SpamFree