Scheduling an Exchange PowerShell Task

by [Published on 8 May 2013 / Last Updated on 8 May 2013]

If you've got a script snippet you wish to run regularly, you can execute it as a scheduled task. We'll look at how to create an Exchange-focused task in this article.

In this article we'll look at a simple example. You want to get some information out of Exchange on a regular basis, and you've been able to put together the Exchange Management Shell command that does what you want.

In our example, we've got a one-liner that exports a CSV file containing the mailboxes on an Exchange server alongside their Mailbox sizes, sorted largest to smallest:

Get-MailboxStatistics -Server exmb01 | Select DisplayName,TotalItemSize | Sort-Descending -Property TotalItemSize | Export-CSV C:\Output\MailboxStats.csv -NoTypeInformation

We can schedule this by opening up the Windows Task Scheduler and choosing Create Basic Task:

 

Follow through the wizard choosing a Task Name, Trigger (for example, daily) and for the action choose Start a Program.

For Program/script enter:

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

Under Add Arguments enter for Exchange 2010:

-version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; <Your Command>"

Or, for Exchange 2013:

-NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; <Your Command>"

You'll need to substitute the path to the Exchange Server installation directory if you've changed if from the defaults, and replace <Your Command> with the Exchange Management Shell cmdlet you wish to schedule. For example:

-version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Get-MailboxStatistics -Server exmb01 | Select DisplayName,TotalItemSize | Sort-Descending -Property TotalItemSize | Export-CSV C:\Output\MailboxStats.csv -NoTypeInformation"

Image 

Finally, on the Finish screen, choose open the "Properties dialog for this task when I click Finish" then click Finish.

In the subsequent Properties dialog box that opens for the new task, ensure enter Security Options you change the radio option to "Run whether the user is logged on or not" and if you need to, change the user account that the task runs as.

You'll then be asked to confirm the password, and yuou can verify the task executes correctly by right-clicking the task and choosing Run:

Image 

Featured Links