Creating Scheduled Tasks for Exchange 2010 PowerShell Scripts


There are many reasons why one would like to run an Exchange powershell .ps1 script as a scheduled task.

In this post I will share with you how to achieve this on both Microsoft Exchange 2007 and Exchange 2010.

Obviously you should start by creating your .ps1 script for this article we will assume the script location is c:\pcs\YourScript.ps1

 

The idea is to run the powershell.exe add pass to it the Exchange powershell console as parameter and then finally specify the desired .ps1 script.

You should notice the difference in the path of the Exchange powershell console between Exchange 2007 and Exchange 2010.

Exchange 2007 exshell.psc1 is located at ""C:\Program Files\Microsoft\Exchange Server\Bin\exshell.psc1""

Exchange 2010 exshell.psc1 is located at ""C:\Program Files\Microsoft\Exchange Server\V14\Bin\exshell.psc1""

Notice the extra V14 in Exchange 2010 ?

So the full command to run would be

For Exchange 2007

PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" -Command ". 'c:\pcs\YourScript.ps1'"

For Exchange 2010

PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V14\Bin\ExShell.psc1" -Command ". 'c:\pcs\YourScript.ps1'"


Scheduling a Task

Now that we've got the syntax figured out, we're ready to schedule a PowerShell task using the Windows Task Scheduler. In this example we'll schedule a task to do a mailbox move after hours, we'll do this from the Exchange server.


First, start the task scheduler and create a new basic task.

Give your task a name, in this example we'll name it "Move Mailbox", click next.

In this example, we are just scheduling a mailbox move, so we'll choose "One time". If you have a script that needs to run daily then adjust the schedule accordingly.


Select the date and time you want to run the script and click next.

Select "Start a program" and click next.

Now paste the entire command into the "Program/Script" field and click next.

The task scheduler will ask if you want to run PowerShell.exe with the arguments specified. Verify that the syntax is correct and click yes.

That will bring you to the last screen, click finish.




dpejic has written 85 articles

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>