First create a powershell *.ps1 script
Test to ensure that you can run the script by running the script from a command prompt (see if you get red errors)
c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File .\My.ps1
Where “My.ps1” is the name/path to your script
Fix any pathing errors before moving on
Now that the script is created you can set it up as a scheduled task.
- 1. On the system that the task will be run from, open the Windows Task Scheduler. This can be found in the Start menu, under Start > Administrative Tools.
- In the Task Scheduler, select the Create Task option under the Actions heading on the right-hand side.
- Enter a name for the task, and give it a description (the description is optional and not required).
- In the General tab, go to the Security options heading and specify the user account that the task should be run under. Change the settings so the task will run if the user is logged in or not.
- Next, select the Triggers tab, and click New to add a new trigger for the scheduled task. This new task should use the On a schedule option. The start date can be set to a desired time, and the frequency and duration of the task can be set based on your specific needs. Click OK when your desired settings are entered.
The below example has the task running every week on Sunday morning at 1 AM, but will stop the task if it runs longer than 4 hours.
- Next, go to the Actions tab and click New to set the action for this task to run. Set the Action to Start a program.
- In the Program/script box enter
In the Add arguments (optional) box enter the following
-ExecutionPolicy Bypass -File .\[Your PowerShell Script Name]
For example, if your PowerShell Script is named “Migration1.ps1″ then you would enter ” -ExecutionPolicy Bypass -File .\[Your PowerShell Script Name].” as the value.
Then, in the Start in (optional) box, add the location of the folder that contains your PowerShell script.
Note: The location used in the Start in box will also be used for storing the scheduled task run times, the job history for the copies, and any additional logging that may occur.
Click OK when all the desired settings are made.
- Next, set any other desired settings in the Conditions and Settings tabs. You can also set up additional actions, such as emailing an Administrator each time the script is run.
- Once all the desired actions have been made (or added), click OK. The task will be immediately set, and is ready to run.
The scheduling of this task is complete, and is now ready to run based on the entered settings.
Right click and run it the first time to ensure the correct effect takes place.
Metalogix article Configuring a PowerShell Script to Run as a Scheduled Task
Squllman’s answer to How can I keep IIS log files cleaned up regularly?