Monday, 19 September 2016

Run/Trigger Powershell Script from Windows Task Scheduler

Windows built in Task Scheduler application can be used to trigger/run powershell scripts on custom schedules as required. Here's how to set it up.

  1. Open Task Scheduler (Control Panel > Administrative Tools > Task Scheduler)
  2. Right click on Task Scheduler Library and select Create Task

  3. Enter a name for the scheduled task. Select the option to Run whether user is logged on or not to ensure the task will still run even if you are not currently logged into the computer

  4. Go to the Triggers tab and click the New button. Set the task to run on the schedule as required (eg. daily, weekly, hourly, etc). I recommend setting the option to Stop the task if it runs longer than and set to 30 mins. This means if there is a bug/problem with the script it will terminate after this time and won't continue running in the background
  5. Select the Actions tab and click the New button. Configure as per below:

    Action: Start a program
    Program/script: C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe
    Add Arguments: (Full path to your powershell script .ps1 file)

    Example path may be D:\Powershell\TestScript.ps1

  6. Other settings and conditions can be configured in their respective tabs as required, but the default settings within here will work

    Note: if you find that your scheduled task won't run, check out My Post on Powershell Execution Policies which may be preventing the scheduled task from running your powershell script

No comments:

Post a Comment