Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.
 

How To Build Scheduled Tasks In PowerShell

How To Build Scheduled Tasks In PowerShell
By

In this article we'll walk you through the process of creating scheduled tasks with PowerShell's ScheduledTasks module instead of the GUI.

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.

MORE: PowerShell Tips & Tricks

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:

  1. Defining the action with New-ScheduledTaskAction
  2. Creating the task trigger with New-ScheduledTaskTrigger
  3. Creating the scheduled task with New-ScheduledTask
  4. 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, lets say we want to execute a PowerShell script with a scheduled task. Our action might look something like this:

$Action = New-ScheduledTaskAction -Execute 'C:WindowsSystem32WindowsPowerShellv1.0powershell.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. If you'd like a good example of this check out the New-ScheduledScript script in the author's Github repository. It demonstrates a few ways you can create functions to run this locally as well as on remote computers.

RELATED:

More PowerShell How To Tutorials