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.
To complete this task, you require the following:
1. A Powershell script. For the purpose of this article, I will call mine samplescript.ps1
2. A Windows Server 2008 (minimum) – The task outlined in this article was carried out on a windows server 2008.
- Copy your script to a folder in your windows server – I will use D:\Scripts\samplescript.ps1 for illustration.
- Open Task Scheduler on your windows server:
- Click start and type task scheduler:
- Click on task scheduler
- The task scheduler window opens
- On the task scheduler window, click Create task to open the Create Task window:
- 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.
- On the Triggers tab, click New… and configure your schedule. A sample is shown below:
- 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
- Beside arguments (optional), enter: Set-ExecutionPolicy Unrestricted -Force. Your script is likely to fail if you do not set execution policy this way. I have tried setting execution policy inside my script but it did not work. This approach works for me.
- 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:
g. 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
- Beside 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\.
- Click Ok, to save your task. If you receive a Task Schedule confirmation, click Yes:
- Note: The default configuration on 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.
I have scheduled a number of tasks to run my PowerShell scripts using this procedure. If this does not work for you or if you experience any problem, please leave questions and / or comments.
This article is available in the following places: