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

  1. Copy your script to a folder in your windows server – let's use D:\Scripts\samplescript.ps1 for illustration.
  2. Open Task Scheduler on your windows server:
    1. Click start and type task scheduler:
    2. Click on task scheduler

  1. The Task Scheduler window opens.

  1. On the Task Scheduler window, click Create Task to open the Create Task window:

  1. 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.
  2. 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.

  1. On the triggers tab, click New… and configure your schedule. A sample is shown below:

  1. When you finish, click OK.
  2. 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.
    1. Click new to add your first action: configure execution policy.
    2. On the Action drop-down, select Start a program.
    3. 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
    4. 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.

  1. When done with your configuration, click OK.
  2. 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:
  3. 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
  4. Besides arguments (optional), enter: .\samplescript.ps1 (Change to your script name but note the dot (.) and the backslash (\)
  5. On the Start in (optional), enter the directory containing your script, for example D:\Scripts\.

  1. Click OK, to save your task. If you receive a Task Schedule confirmation, click Yes:

Note: The default configuration of the Conditions and Settings tabs are okay but you may configure it if you wish.

  1. 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