Scheduled tasks are one of those topics that nearly every system administrator has to deal with. Scheduled tasks are awesome at doing something based on various triggers. But scheduled tasks have a lot of knobs to turn and switches to flip if configured correctly.

If you're building an automation script, one way to quickly create scheduled tasks is to use PowerShell. Using PowerShell to create scheduled tasks allows you to quickly create scheduled tasks that are standardized and free of the human fat-finger syndrome. You might even learn how to run a PowerShell script from the task scheduler too!

Scheduled tasks have schedules of insane complexity, lots of different trigger options, various security contexts they can run under and a lot more. Where complexity reigns configuration errors always crop up. We, humans, are messy and are prone to make mistakes given many different options. This is why creating scheduled tasks are ripe for automation and what better language to do that in is PowerShell!

Scheduled task management in PowerShell got a lot easier with the introduction of Windows 8.1 and Server 2012 R2. Lots of new cmdlets were introduced that made this job much easier to manage. We'll be focusing on this method in the article.

Creating Scheduled Tasks the Old Way

However, if you're still unfortunate enough to be working with older operating systems, managing scheduled tasks is still possible with PowerShell. However, you'll need to look at articles like this or investigate the schtasks utility. Life will be much harder for you but still possible.

Creating Scheduled Tasks the Modern Way

At a minimum, a scheduled task has an action, a trigger and a group of associated settings. Once the task is created, it then needs to be registered on the system to be created. You'll need to perform each of these actions separate just to create a single scheduled task.

To create the action, use the New-ScheduledTaskAction command. This is where the command to run is specified.

Since you're building scheduled tasks in PowerShell, take it a step further and create a scheduled task that invokes a PowerShell script itself. Below I'm invoking the PowerShell engine and passing a script to it using all of the appropriate command line switches to make the script run non-interactively.

The code below the C:\MyScript.ps1 exists on the machine the scheduled task is running on. You can test this command out manually if you'd like by opening up a command prompt (cmd.exe) and running powershell.exe -NonInteractive -NoLogo -NoProfile -File "C:\MyScript.ps1"

$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NonInteractive -NoLogo -NoProfile -File "C:\MyScript.ps1"'

Next, you need a trigger. A lot of different values can be used here, but below you're just going to execute this script once at 3 AM. For a full list of options, check out the help page.

$Trigger = New-ScheduledTaskTrigger -Once -At 3am

Next, to create the scheduled task, you use the New-ScheduledTask command. This command requires a value for the Settings parameter. Even if you're not using any special settings, this parameter is still required. This is why you run New-ScheduledTaskSettingsSet to create an object to pass in here.

$Settings = New-ScheduledTaskSettingsSet

Now that you have everything gathered up, you'll pass each of these variables to the New-ScheduledTask command to create a scheduled task object.

$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings

However, you haven't created the scheduled task on the system yet. You have one more step to go, and that step is registering the scheduled task using the Register-ScheduledTask command.

PS> Register-ScheduledTask -TaskName 'My PowerShell Script' -InputObject $Task -User 'username' -Password 'passhere'

Actions            : {MSFT_TaskExecAction}
Author             :
Date               :
Description        :
Documentation      :
Principal          : MSFT_TaskPrincipal2
SecurityDescriptor :
Settings           : MSFT_TaskSettings3
Source             :
State              : 3
TaskName           : My PowerShell Script
TaskPath           : \
Triggers           : {MSFT_TaskTimeTrigger}
URI                : \My PowerShell Script
Version            :
PSComputerName     :

Notice that I chose to run this scheduled task under a particular username. When running under a certain user's context, you'll also have to provide the password as well. You've got lots of different options here. The help for Register-ScheduledTask is excellent in this area.

Here's the full script to create a scheduled task:

$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-NonInteractive -NoLogo -NoProfile -File 'C:\MyScript.ps1'"
$Trigger = New-ScheduledTaskTrigger -Once -At 3am
$Settings = New-ScheduledTaskSettingsSet
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
Register-ScheduledTask -TaskName 'My PowerShell Script' -InputObject $Task -User 'username' -Password 'passhere'

Once the scheduled task is registered, you should now be able to see it using Get-ScheduledTask.

Summary

In this post, you learned how to use PowerShell to create scheduled tasks and, as the example, also learned how to run a PowerShell script from task scheduler via the scheduled task. If you're building an automation script of some kind and need to quickly create scheduled tasks or want a quick, command-line way of building scheduled tasks, check out PowerShell!