How to schedule a Powershell script using Scheduled tasks in Windows Server 2008 R2

Powershell is a really powerfull tool but then you have to know how to schedule your scripts using the task scheduler if you would like to run script on a daily base for example.

Open the task scheduler (Start -> All Programs -> Accessories -> System Tools -> Task Scheduler

Click op Create Basic Task

Type a name and description for your task

Choose your trigger

And configure your trigger

Configure the action “Start a program”

The program to start is Powershell

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

with the arguments:

-command “& ‘D:\yourfilewithcommands.ps1’ “

UPDATE:

If you don’t like to change the execution policy for the computer you can use:

-ExecutionPolicy Bypass -file “D:\yourfilewithcommands.ps1”

Click the open the properties window checkbox to set extra options

Select the Run wheter user is logged on on not option and check the Run with highest privileges checkbox, don’t forget to select the correct user!

On the Triggers tab you can fine tune your schedule to repeat the schedule every hour, 10 minutes or whatever

Save the Scheduled task and now you can run it manually (or wait for it to run automatically if you like) to test it.

Now you have a scheduled powershell script

11 comments on “How to schedule a Powershell script using Scheduled tasks in Windows Server 2008 R2”

  1. Heather says:

    “How to schedule a Powershell script using Scheduled tasks in Windows Server 2008 R2 | MicrosoftPro.
    nl” actually got myself simply addicted with ur blog! Iwill probably
    be back again considerably more often. With thanks -Penelope

  2. Josh says:

    You do realize that “you’re” means “you are” right?

    1. Michael says:

      Hi Josh,

      I do realize that, I wasn’t aware I had the typo still in my blog thanks for the reminder.

  3. Brian says:

    I don’t know how many variations of -command “& ‘D:\yourefilewithcommands.ps1′ “
    I have seen, but this is the first one that worked. I tried probably 6 different ways to run a powershell script as a scheduled task in 2008 R2 and could not get it to actually run until using the single quote in the quotes.

  4. Josh says:

    Thanks so much Michael, very simple and very effective.

  5. Paramesh says:

    Hi Michael,

    Tried scheduling the Powershell script using Task scheduler but not sure what is wrong but scheduler is not able to kick my powershell script. When checked task history it shows that task has been completed successfully.

    Maintained the same parameters in the argument space of the Trigger tab. Any help is really appreciated.

    1. Michael says:

      Hi Paramesh,

      I don’t know what you try to accomplish, but if the history says the script runs my guess is it runs.

      Is it possible that the user which you use to execute the taak doesn’t have the correct permissions to perform the tasks in the powershell script?

      Regards,

      Michael

  6. Matt says:

    Perfect!

  7. Jerry Phillips says:

    I am the Exchange Admin and can run the Queue monitor script manually with no problems. I receive the alert email with no problems. When I tried scheduling the Powershell script using Task scheduler it tells me the script completed normally , but I never receive the alert email. I tried both the arguments, same result. I’m running Exchange 2010 on a Windows 2008 R2 Enterprise Server. Any Ideas

    1. Michael says:

      Hi Jerry, I’m really sorry for the late response. This could be a number of reasons but make sure the account you are using to schedule the script has sufficient permissions to run the script and permissions within Exchange.

Leave a Reply to Michael Cancel reply

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

captcha

Please enter the CAPTCHA text

This site uses Akismet to reduce spam. Learn how your comment data is processed.