Create Windows Scheduler Task By PowerShell Automation

In this article, we will see simple steps to configure Windows Scheduler tasks using PowerShell Automation. This article can be useful in conjunction with some of the earlier articles that I have written on Process Automation using PowerShell.

Before getting into the code, let us see the Windows Task Manager in order to understand where we can find tasks which are getting created by the code.

Type “Task Manager”.

1

You can see the highlighted section where you can find newly created tasks.

2

The code is pretty simple and based on standard PowerShell Commandlets, as described below.

3

In Step 1, we are defining Trigger for the Task by using “New-ScheduledTaskTrigger” Commandlet. It could be any permissible Time Unit as shown below.

4

In Step 2 & 3, we are setting up the Username and Password that the Task will use as owner account to run the defined action.

In Step 4, we are defining the action by using “New-ScheduledTaskAction” Commandlet that needs to be executed by the task when the trigger is reached. In this demo, this task will execute a PowerShell script.

Finally, in Step-5, we are registering the tasks by using “Register- ScheduledTask” Commandlet. This Commandlet takes Name, Trigger, UserName, Password, and Action as input parameters (as defined in the above steps).

Once the script gets executed, a new task will be added to the Windows Task Scheduler.

5

We can see the new task added in Task Scheduler window, as shown below.

6

If we double click this task, it will open the Task Configuration screen that we can use to verify the Task Properties, as explained below.

General Tab: Here, we can see the name of the Task & User Account that will be used to execute the defined action.

7

Triggers Tab: Here, we can see the Trigger defined for this Task.

8

Actions Tab: Here, we can see the action defined for this task. In this demo, we have specified a PowerShell Script to be executed whenever the respective trigger is achieved.

9

In case you make any changes to this task, Windows will present you a password dialog to confirm the user credentials. If asked, enter the respective credentials.

10

This is a simple implementation that allows you to configure Windows Tasks using PowerShell automation.

That is all for this demo.

Hope you will find it helpful.