PowerShell – Build Scheduled Tasks


To totally unlock this section you need to Log-in

Building a single scheduled task via the GUI task scheduler might not be a big deal. Click through the dozens of options to fill in the name, description, security options, time to execute, etc. If you have to do it once, it's easy and doesn't take a whole lot of time.

But if you find yourself creating scheduled tasks repeatedly it might be a good idea to use a method that scales better than a GUI -- like PowerShell. Using PowerShell you can create scheduled tasks on your local or remote computers without a lot of effort. Today, we'll show you how to create a scheduled task with PowerShell.

PowerShell v4 introduced a ScheduledTasks module that greatly simplified the times of creating Task Scheduler COM objects to build scheduled tasks from the command line. This is what we'll be going over today. If you're not on PSv4 yet but you have a need to create scheduled tasks often, this module alone should be the catalyst for you to get upgraded.

You might think that you could simply run New-ScheduledTask to create a scheduled task -- you'd be wrong. Due to all the moving parts that come with a scheduled task you must define a lot of different settings prior to actually creating the task, including:

  • Defining the action with New-ScheduledTaskAction
  • Creating the task trigger with New-ScheduledTaskTrigger
  • Creating the scheduled task with New-ScheduledTask
  • Registering the scheduled task with Register-ScheduledTask

Every scheduled task must have an action associated with it; a file to execute. First, you need to run New-ScheduledTaskAction and assign that action to a variable. For demonstration purposes, let's say we want to execute a PowerShell script with a scheduled task. Our action might look something like this:

$Action = New-ScheduledTaskAction -Execute 'C:\Windows\System32\Windows PowerShell v1.0\powershell.exe' -Argument "-NonInteractive -NoLogo -NoProfile -File 'C:\SomePowerShellscript.ps1'"

Since we want to run a script, we have to invoke powershell.exe and pass the script as an argument, so we use the -Argument parameter.

Next, we need to define how the task is going to be triggered by creating a trigger with New-ScheduledTaskTrigger. We'd like this task to run every day at 3AM.

$Trigger = New-ScheduledTaskTrigger -Daily -At '3AM'

Now, we need to actually created the scheduled task but this simply stores it in memory and doesn't actually create it yet. That's what Register-ScheduledTask is for in our next step. For this step, you'll use the action and trigger you just created and a blank scheduled task settings object is required, which we'll created using the New-ScheduledTaskSettingsSet cmdlet.

$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet)

Once the entire scheduled task has been built we can finally create it on the system with the Register-ScheduledTask cmdlet. In this step, we'll pass the $Task object to Register-ScheduledTask and fill in some further information that's required to get the task created, not included in the $Task object like the name of the task and the credentials to run it under.

$Task | Register-ScheduledTask -TaskName ‘My PowerShell script -User 'administrator' -Password 'supersecret'

We're finally done! That may seem like a lot to build a scheduled task but we assure you this is much easier than it ever has been -- and much faster than using the GUI each time.