The process for scheduling Exchange 2010 PowerShell scripts with Windows Task Scheduler is different that what you may be used to in Exchange 2007. This is because Exchange 2010 uses PowerShell remoting for Exchange Management Shell administration. In this post I’ll go over how you can schedule PowerShell scripts for Exchange 2010 tasks.
Here are a few things you’ll want to take note of before getting started.
- Tasks can be scheduled with or without the Exchange tools installed – since all PowerShell management is done via remoting, we can schedule the tasks to run on a workstation, or on an Exchange server.
- Script signing – as a best practice, you may want to consider signing your scripts.
- Execution Policy – the PowerShell execution policy is set to remotesigned automatically when you install Exchange. You may need to set your execution policy if you’ll be scheduling tasks to run from a machine without the Exchange tools installed.
PowerShell Command Syntax for Scheduling Tasks on a Machine with the Exchange Management Tools Installed
The following command syntax is what you’ll want to use to schedule your task. You need to tell PowerShell to load the Exchange Management Shell environment before executing your script. This will give you access to all of the cmdlets, variables and functions that are loaded with Exchange Management Shell. To schedule a .ps1 script to move mailboxes, the syntax would look something like this:
As you can see in the example, we’re using PowerShell code inside the command parameter. If you don’t want to use a .ps1 script you can embed your code within this parameter. Separate each line of PowerShell code using a semi colon. You might find this method useful for short scripts or one-liners. For example, say I want to schedule some mailbox moves from one DAG database to another, the syntax might look like this:
These examples assume you used the default installation path for Exchange 2010; you may need to modify these commands to match your configuration.
PowerShell Command Syntax for Scheduling Tasks on a Machine Without the Exchange Management Tools Installed
To schedule PowerShell scripts we don’t actually need the Exchange tools installed, all we need is PowerShell v2. We can use implicit remoting to import the Exchange Management Shell cmdlets from a specific server. Take a look at the syntax:
This is very similar to the previous example as we specify all of our code within the command parameter. This time we create a new PSSession, import the PSSession, and then perform the mailbox move.
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.
There you go, you now have an Exchange PowerShell task scheduled to run.
If you have User Account Control (UAC) enabled, you may need to enable the option to Run with highest privileges in the properties of the scheduled task. Also, you will probably want to enable the option to Run whether user is logged on or not in the properties of the scheduled task.