Running Exchange 2010 PowerShell Scripts Using Task Scheduler

I have a number of complex PowerShell scripts for performing employee onboarding/offboarding, mailbox export request cleanup, and other tasks. Scripts that use the standard cmdlet set, the Active Directory module, or Quest Activeroles AD Management snapin worked fine, but Exchange 2010 and Lync 2010 cmdlets didn’t despite loading the Exchange Management and Lync snapins. Those cmdlets didn’t seem to do anything at all and the scheduled task sometimes wouldn’t end even if I included an ‘exit’ at the end.

There were two important changes I needed to make in arguments of the scheduled task that, together, fixed both problems.

Scheduling an Exchange 2010 PowerShell script via Task Scheduler

-version 2.0 -noexit -command “. ‘C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto;C:\Windows\System32\WindowsPowerShell\v1.0\MyScript.ps1; exit

The first part, in red, establishes the connection to the Exchange 2010 server with the correct PowerShell version number (2.0) and parameters.

The second part, the little green “exit” at the end of the arguments, forces the task to end when the script is complete.

This change solved a problem that I had puzzled over, off and on, for months. I hope it saves you some headaches too.

I haven’t tested it on Exchange 2013, but the arguments are probably very similar. Just adjust for version numbers.

Leave a Reply

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