How do I run a PowerShell script as a scheduled task?

green

Creating a scheduled task to run a PowerShell script every X hours/days is pretty straight forward.  There a couple of things to be aware of:

Save your script with a .ps1 extension.

Ensure all dependent modules have been installed on the server you’re running the task on (ie; if you need the Active Directory modules, make sure you have installed the RSAT tools).

Schedule the task to run powershell.exe with the “-f” switch followed by the name of the .ps1 file you saved:

schedule

Set the task to “Run whether user is logged on or not”:

schedule2

And unless you know your script takes longer than an hour to run, I always set it to timeout after an hour:

schedule3

There are two other requirements that often come up, outside of the Task Scheduler, that will stop your script from running as expected.

The account running the Scheduled Task will need “Log on as a batch job” rights. (http://technet.microsoft.com/en-us/library/cc957131.aspx)

You will likely need to change the Remote Execution Policy to “Remote Signed” with this command:

Note: Changing the remote execution policy has security implications that you need to consider before making the change.

green

Leave a Reply

Your email address will not be published. Required fields are marked *