Windows Server: How to schedule a PowerShell Script to auto run
Introduction
Windows Administrators create PowerShell scripts to automate certain routine admin tasks. After creating these scripts, you might need to run the script on a regular schedule. This article will show you how to configure your PowerShell script to run on regular basis using Windows Task Scheduler. It will cover dealing with issues relating to execution policy, run as account and much more.
Requirements
To complete this task, you require the following:
1. A PowerShell script. For the purpose of this article, let's call it samplescript.ps1
2. A Windows Server 2008 (minimum) – The task outlined in this article was carried out on a Windows Server 2008.
Procedure
- Copy your script to a folder in your windows server – let's use D:\Scripts\samplescript.ps1 for illustration.
- Open Task Scheduler on your windows server:
- Click start and type task scheduler:
- Click on task scheduler
http://www.itechguides.com/wp-content/uploads/2014/09/search_task_scheduler.png
- The Task Scheduler window opens.
http://www.itechguides.com/wp-content/uploads/2014/09/task_scheduler_first_window.png
- On the Task Scheduler window, click Create Task to open the Create Task window:
http://www.itechguides.com/wp-content/uploads/2014/09/create_task_general_window.png
- Enter a name and description for your task. The general tab allows you to configure security options – Under what security context will the task run, will the task only run when a user is logged on or not (select Run whether a user is logged on or not). You can also configure whether this task is scheduled for a Windows 7, Windows Server 2008 or Windows Server 2003, Windows XP, or Windows 2000. Select Windows 7, Windows Server 2008.
- Click Triggers tab. This tab allows you to configure what triggers the task. Effectively, you are configuring what will make the task run. This is where you configure the actual scheduling.
http://www.itechguides.com/wp-content/uploads/2014/09/create_task_triggers_window.png
- On the triggers tab, click New… and configure your schedule. A sample is shown below:
http://www.itechguides.com/wp-content/uploads/2014/09/create_task_triggers_window_new_trigger.png
- When you finish, click OK.
- Click Actions tab: This is where the main task for this article is configured. In the actions tab, you will configure your PowerShell script to run. But you will have to add an action that sets your execution policy first.
- Click new to add your first action: configure execution policy.
- On the Action drop-down, select Start a program.
- On the Program/script windows, type the full path to your PowerShell installation – Most likely to be: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- Besides arguments (optional), enter: Set-ExecutionPolicy Unrestricted –Force. Your script is likely to fail if you do not set execution policy this way. We have tried setting execution policy inside my script but it did not work. This approach works for me.
http://www.itechguides.com/wp-content/uploads/2014/09/create_task_actions_window_new_action_1.png
- When done with your configuration, click OK.
- On the actions tab, click new to add your second action: configure your PowerShell script to run. Actions run in sequence, so your script will run after the set execution policy action has completed:
- On the Program/script windows, type the full path to your PowerShell installation – Most likely to be: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- Besides arguments (optional), enter: .\samplescript.ps1 (Change to your script name but note the dot (.) and the backslash (\
- On the Start in (optional), enter the directory containing your script, for example D:\Scripts\
http://www.itechguides.com/wp-content/uploads/2014/09/create_task_actions_window_new_action_2.png
- Click OK, to save your task. If you receive a Task Schedule confirmation, click Yes:
http://www.itechguides.com/wp-content/uploads/2014/09/create_task_actions_window_new_action_3.png
Note: The default configuration of the Conditions and Settings tabs are okay but you may configure it if you wish.
- Now that you have added all your parameters, on the Create Task window, click OK, to save your changes.
Conclusion
We have scheduled a number of tasks to run my PowerShell scripts using this procedure. If this does not work for you, please leave questions and / or comments.
Reference
This article is also available in the following places: iTechguides.com