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 written on Process Automation using PowerShell
Before getting into the code, let see the Windows Task Manager to understand where we can find tasks which are getting created by the code
Type “Task Manager”
https://howtodowithsharepoint.files.wordpress.com/2017/01/1.png?w=800
You can see the highlighted section where you can find newly created Tasks
https://howtodowithsharepoint.files.wordpress.com/2017/01/2.png?w=800
The code is pretty simple and based on standard PowerShell cmdlets as described below:
https://howtodowithsharepoint.files.wordpress.com/2017/01/3.png?w=800
In Step 1, we are defining Trigger for the Task by using “New-ScheduledTaskTrigger” cmdlet. It could be any permissible Time Unit as shown below
https://howtodowithsharepoint.files.wordpress.com/2017/01/4.png?w=800
In Step 2 & 3 we are setting up User Name and Password that the Task will be used as Owner Account to run the defined action
In Step 4 we are defining the action by using “New-ScheduledTaskAction” cmdlet that needs to be executed by the task when trigger reached, in this demo, this task will execute a PowerShell script
Finally, in Step-5 we are registering the tasks by using “Register- ScheduledTask” cmdlet. This cmdlet takes Name, Trigger, UserName, Password and Action as an input parameter (as defined in above steps)
Once the script gets executed a new task will be added to the Windows Task Scheduler
https://howtodowithsharepoint.files.wordpress.com/2017/01/5.png?w=800
We can see the new task added in Task Scheduler Window as shown below:
https://howtodowithsharepoint.files.wordpress.com/2017/01/6.png?w=800
If we double- click this task, it will open the task configuration screen that we can use 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
https://howtodowithsharepoint.files.wordpress.com/2017/01/7.png?w=800
**Triggers Tab: **Here we can see the Trigger defined for this task
https://howtodowithsharepoint.files.wordpress.com/2017/01/8.png?w=800
**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 respective trigger achieved
https://howtodowithsharepoint.files.wordpress.com/2017/01/9.png?w=800
In case if 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
https://howtodowithsharepoint.files.wordpress.com/2017/01/10.png?w=800
This is a simple implementation that allows us to configure Windows Tasks using PowerShell Automation.
That is all for this demo.