Run PowerShell script as a scheduled task

You are here:
< Back

 Goal

The goal of this article is to provide clear, concise method for running PowerShell scripts using Windows task scheduler.  Running a PowerShell script using the task scheduler may not be as straight forward as one might expect.  In fact, it is quite different from running standard shell scripts.  This article should provide the generic syntax for running PowerShell scripts via task scheduler as well as some basic troubleshooting when things go wrong.

Background

PowerShell is Microsoft’s new scripting language that allows administrators to perform almost any task.  It is becoming more and more a requirement of the professional administrator.  Getting simple things to play nice still seems to be a small issue.

The following links were used in creating this article.

http://www.mikepfeiffer.net/2010/02/creating-scheduled-tasks-for-exchange-2010-powershell-scripts/

http://social.technet.microsoft.com/Forums/windowsserver/en-US/43e19576-476d-4735-85ef-c508c18f11e8/executing-powershell-scripts-via-task-scheduler

Resolution

In order for a scheduled task to be successful when running a PowerShell script, the location / path of the script cannot simply be provided.  The executable of PowerShell needs to be the program to run, with the path to the script to be run submitted as the arguments.  Occasionally, it might be required to provide the full path for the PowerShell executable, but usually simply supplying PowerShell.exe should do the trick.  The following is the syntax that can be used if supplying the whole command on the program line.  Task scheduler will normally recognize this and ask if you want to place the arguments in the arguments section.  This will accomplish the same thing as manually separating the command before entering it.

powershell.exe "& 'C:\scripts\powershell_script.ps1'"

At times, using the following parameter is required to allow an unsigned script to run correctly.

-executionpolicy bypass

This parameter tells PowerShell not to look at the script to validate it, but simply to run it.

Troubleshooting

The following are common issues preventing a PowerShell script from running successfully.

  • Does the script require the “-executionpolicy bypass” parameter to bypass the signing checks?This is probably the most sneeky of the bunch.  It may appear that everything is running successfully, but nothing actually ever happens.
  • Does the script require elevated privlages to run successfully?Some PowerShell scripts will only run with elevated privileges.  Verify that the check box on the first page of the schedule task configuration windows is checked for “run with highest privileges”.
  • Is the syntax correct for executing the script?The easiest way to verify you have the correct syntax is to run it from the run dialog.  If the script fails here, the problem is not with the scheduled task, but with the syntax of the command itself.
Last Updated On October 24, 2017