Manage Scheduled Tasks in Windows 8 and Windows Server 2012 with PowerShell : Part 2

In the first article of this series, I showed you how to use the ScheduledTasks module to retrieve information about scheduled tasks. This requires PowerShell v3 but is only available on Windows 8 and Windows Server 2012. However, you can use module cmdlets to remotely manage tasks on either platform.

In this article I want to walk through creating a new scheduled task using PowerShell. Eventually we will use the New-ScheduledTask cmdlet, and it is possible to create a task with a complex one-line command. But for the sake of clarity I’m going to break things down step-by-step. Once you understand the concepts and have some experience you can throw everything together in a single command.

Defining a Trigger

The first item of business is to define a scheduled task trigger. That is, when do we want the task to run? The cmdlet to use is New-ScheduledTaskTrigger. You can see the help in Figure 1.

New Scheduled Tasks

Sponsored Content

Passwords Haven’t Disappeared Yet

123456. Qwerty. Iloveyou. No, these are not exercises for people who are brand new to typing. Shockingly, they are among the most common passwords that end users choose in 2021. Research has found that the average business user must manually type out, or copy/paste, the credentials to 154 websites per month. We repeatedly got one question that surprised us: “Why would I ever trust a third party with control of my network?

I am not going to go through every parameter option, as most of the parameters should be self-explanatory. But I do want to point out the –At parameter. You can specify any date or time string, and the cmdlet will try to convert it into an appropriate [DateTime] object.

PS C:\> new-jobtrigger -Once -At "18:00"   Id         Frequency       Time                   DaysOfWeek          Enabled --         ---------       ----                   ----------          ------- 0          Once            10/3/2012 6:00:00 PM                          True PS C:\> new-jobtrigger -Once -At "12/15/2012 8:30"   Id         Frequency       Time                   DaysOfWeek          Enabled --         ---------       ----                   ----------          ------- 0          Once            12/15/2012 8:30:00 AM                         True

Here’s an example of a weekly trigger that is set for Monday, Wednesday and Friday at 11:55 PM.

​PS C:\> new-jobtrigger -Weekly -DaysOfWeek Monday,Wednesday,Friday -at 11:55PM


Id        Frequency       Time                   DaysOfWeek           Enabled

--        ---------       ----                   ----------           -------

0         Weekly          10/3/2012 11:55:00 PM  {Monday, Wednesday, ... True

I’ve just been writing these examples to the pipeline so you can see what they look like. For my new task I’ll create a trigger and save it to a variable.

PS C:\> $t=new-scheduledtasktrigger -daily -at 12:00PM

Defining an Action

Next, I need to define what I want my task to do. This is accomplished with the New-ScheduledTaskAction cmdlet.


New-ScheduledTask Help

I want to create a task to create an msinfo report for the local computer in the C:\Work directory.

PS C:\> $a=new-scheduledtaskaction -execute "msinfo32.exe" -argument "/report c:\work\%computername%-msinfo.txt" -workingdirectory "c:\work"

The value for the –Execute parameter is the path to the program I want to run. Because it is in the system32 directory I can skip using the full path. The –Argument parameter contains the command line arguments. Notice I’m using the legacy environmental variable %Computername% instead of a PowerShell version. Even though I am creating this in PowerShell, the command will actually run in a CMD.EXE session. Don’t forget that all of this is relative to the computer that will run the task: If you are creating a scheduled task on a remote computer, then all files and paths are on THAT computer, not yours.

Specify Credentials

Next, I need to specify the credentials I intend to use for the scheduled task. When I get to registering a task I could simply enter the user name and password as parameter values. But you may want to use variables. You could do this:

PS C:\> $username = "globomantics\administrator" PS C:\> $password = "[email protected]"

If you are working interactively in a secure setting this is probably okay. But another approach to try, especially if you are creating a scheduled task from a script, is to prompt for credentials.

$cred = get-credential "$env:computername\administrator" $username = $cred.username $password = ($cred.GetNetworkCredential()).password

Yes, $password is in clear text but only for as long as your script is running and only in memory, so it should be pretty secure. You should not hardcode any passwords in your script.

Register the Task

Finally, we are ready to register the task using the Register-ScheduledTask cmdlet. To keep things flexible, I’ll assign the task name to a variable.


PS C:\> $name="DailyMSInfo"

PS C:\> Register-ScheduledTask -TaskName $name -Trigger $t -Action $a -User $username -Password $password


TaskPath                                       TaskName                 State

--------                                       --------                 -----

\                                              DailyMSInfo              Ready

The task will be created in the root folder of the task scheduler as you can see below in Figure 3.


newly scheduled task

You can create tasks in other folders in the task scheduler using the –TaskPath parameter.

PS C:\> Register-ScheduledTask -TaskName $name -Trigger $t -Action $a -User $username -Password $password -TaskPath MyCompany\Reporting   TaskPath                                       TaskName                State --------                                       --------                ----- \MyCompany\Reporting\                          DailyMSInfo             Ready

The cmdlet will create the path if it doesn’t exist.


In only takes a few quick steps to create a scheduled task using PowerShell. If you want to push the task out to remote computers, create all the settings on your computer then register it using the –CIMSession parameter, and specify the names of the Windows 8/Windows Server 2012 system. You can create scheduled tasks using either an interactive console, or a script although be careful about how you handle passwords. In part three of this series, we’ll look at managing this task.

Related Topics:

Don't leave your business open to attack! Come learn how to protect your AD in this FREE masterclass!REGISTER NOW - Thursday, December 2, 2021 @ 1 pm ET

Active Directory (AD) is leveraged by over 90% of enterprises worldwide as the authentication and authorization hub of their IT infrastructure—but its inherent complexity leaves it prone to misconfigurations that can allow attackers to slip into your network and wreak havoc. 

Join this session with Microsoft MVP and MCT Sander Berkouwer, who will explore:

  • Whether you should upgrade your domain controllers to Windows Server
    2019 and beyond
  • Achieving mission impossible: updating DCs within 48 hours
  • How to disable legacy protocols and outdated compatibility options in
    Active Directory

Sponsored by: