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:
Set the task to “Run whether user is logged on or not”:
And unless you know your script takes longer than an hour to run, I always set it to timeout after an hour:
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:
set-executionpolicy remotesigned -scope localmachine
Note: Changing the remote execution policy has security implications that you need to consider before making the change.