Maksim Kabakou - Fotolia

Get started Bring yourself up to speed with our introductory content.

Learn how to create a scheduled task with PowerShell

When you need a reliable way to create a scheduled task, you can use PowerShell’s automation capabilities to reduce the likelihood of keying in mistakes.

Nearly every system administrator has to deal with scheduled tasks. They are incredibly helpful to do something based on various triggers, but they require a lot of manual effort to configure properly.

The benefit of scheduled tasks is you can build one with a deep level of sophistication with trigger options and various security contexts. But where complexity reigns, configuration errors can arise. When you're developing these automation scripts, you can create a scheduled task with PowerShell to ease the process. Using PowerShell helps standardize the management and setup work involved with intricate scheduled tasks, which has the added benefit of avoiding the usual errors that stem from manual entry.

Build a scheduled task action

At a minimum, a scheduled task has an action, a trigger and a group of associated settings. Once you create the task, you also need to register it on the system. You need to perform each action separately to create a single scheduled task.

To create the action, use the New-ScheduledTaskAction cmdlet which specifies the command to run. Let's create an action that gets a little meta and invokes a PowerShell script.

The command below gives an example of invoking the PowerShell engine and passing a script to it using all of the appropriate command line switches to make the script run noninteractively. The script file resides on the machine the scheduled task will be running on.

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

Create a trigger

Next, you need a trigger. You have a several values available, but this task will use a specific time -- 3 a.m. -- to execute this script once. For a full list of options, check out the New-ScheduledTaskTrigger cmdlet help page.

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

Create settings

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

$Settings = New-ScheduledTaskSettingsSet

Create the scheduled task

After assigning all the objects as variables, 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

Register the scheduled task

At this point, you have created a scheduled task object in memory. To add the scheduled task on the computer, you must register the scheduled task using the Register-ScheduledTask cmdlet.

The example below registers a scheduled task to run under a particular username. To run the task under a certain user's context, you have to provide the password. It's helpful to look at the documentation for the Register-ScheduledTask command to see all the options to use with this cmdlet.

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

If you followed all the steps correctly, you should see your newly created scheduled task in the Task Scheduler.

scheduled task settings
After you create a scheduled task with PowerShell, you can open the Task Scheduler to see the name of the task, its triggers and other details.

After the scheduled task is registered and showing up in Task Scheduler, you can now run the Get-ScheduledTask cmdlet to see the task.

PS C:\> Get-ScheduledTask -TaskName 'My PowerShell Script'
TaskPath                                       TaskName                          State
--------                                       --------                          -----
\                                              My PowerShell Script              Ready

Dig Deeper on Windows administration tools

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchSQLServer

SearchEnterpriseDesktop

SearchVirtualDesktop

Close