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
Type a name and description for your task
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
“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
You do realize that “you’re” means “you are” right?
Hi Josh,
I do realize that, I wasn’t aware I had the typo still in my blog thanks for the reminder.
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.
Thanks so much Michael, very simple and very effective.
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.
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
Perfect!
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
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.