Managing Scheduled Tasks, Part 2: Create A New Task
Last time, we looked at how ScheduledTasks works. This time, let's create a new scheduled task using -- wait for it -- New-ScheduledTask.
- By Jeffery Hicks
In the last lesson we started looking at using the PowerShell 3.0 and the ScheduledTasks module which we'll find in Windows 8 and Windows Server 2012. This time, we look at creating a new scheduled task. Remember, with PowerShell 3.0 you can simply use any module cmdlet without having to import the module. The command we'll need is New-ScheduledTask.
It is possible to create everything you need in one complex PowerShell expression, but I'm going to break it down into several steps that we can combine at the end. There's nothing wrong with this approach and certainly makes sense if you are building a script.
First, let's define the task we want to run. This can be anything that can be expressed as a command line expression. It doesn't have to be PowerShell related. I want to use the MSINFO32.EXE tool to create a system information report for the computer, saving the results to a text file. From the CMD prompt, this is the command I want to run:
C:\> msinfo32 /report c:\work\%computername%-msinfo.txt
I'll create a new Actions object with New-ScheduledTaskAction in an elevated PowerShell session:
PS C:\> $action=new-scheduledtaskaction -execute "msinfo32" -argument "/report c:\work\%computername%-msinfo.txt" -workingdirectory "c:\work"
Here's what I end up with:
PS C:\> $action
Arguments : /report c:\work\%computername%-msinfo.txt
Execute : msinfo32
WorkingDirectory : c:\work
There is are scheduled task options I could set, but I'll stick to the defaults. Next, I need a trigger to define when I want this task to run, such as every day at noon.
PS C:\> $trigger=new-scheduledtasktrigger -daily -at 12:00PM
PS C:\> $trigger
Id Frequency Time DaysOfWeek Enabled
-- --------- ---- ---------- -------
0 Daily 5/22/2012 12:00:00 PM True
Finally, I need to specify what account it will run under. There is a New-ScheduledTaskPrincipal cmdlet, but in the current beta releases it is problematic. But we can still create the scheduled task because we have to specify a user name and password anyway, which will become the ScheduledTaskPrincipal:
PS C:\> Register-ScheduledTask -TaskName MSInfoReport -Trigger $Trigger -Action $action -description "Create Daily MSInfo report" -User "Win8CP\Administrator" –Password P@ssw0rd
This registers the task and it will run under the local administrator account. I can verify this by getting the principal:
PS C:\> Get-ScheduledTask MSInfoReport | Select -ExpandProperty Principal
Id : Author
LogonType : Password
RunLevel : Limited
UserId : WIN8CP\Administrator
ProcessTokenSidType : Default
The task is created in the root of the Task Scheduler because I didn't specify a path (see Fig. 1).
Figure 1. A new scheduled task. (Click image to view larger version.)
Next time we'll look at managing these scheduled tasks such as manually starting a task, disabling a task, modifying settings and removing a task.
Note: This article is based on a pre-release versions of PowerShell 3.0 and Windows 8.
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.