Run Exchange Management Shell script via Task scheduler

Trying to run Exchange management Shell script on a schedule.

Scenario: I have Exchange scripts stored in E:\Scripts\Get-mailboxReport.ps1

Script is basically pulls mailbox report. If I run the script manually, I perform  below:

.\Get-MailboxReport.ps1 -server Exchange01.contoso.local -filename c:\mailboxreport.csv
which generates a csv file in the specified directory.

I want to run this script using task scheduler, also I need to send this CSV file via email.

Please give me directions on how should I approach this

Thank you
Tech ManInformation TechnologyAsked:
Who is Participating?
Tech ManInformation TechnologyAuthor Commented:
First blog post is describing how to run Powershell via task scheduler. In my scenario, I have to run PowerShell for Exchange 2016( exchange management shell) plus exchange script has parameters that I have to pass.
J SSenior Systems AdministratorCommented:
You can create a task using the following parameters on a new action under the Action tab

Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add Arguments (Optional): -command “. ‘C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; . ‘C:\yourscript.ps1’   ‘input parameters’
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
Well, this is the thing.

If you open a regular PowerShell console, you won't get the regular exchange cmdlets active into that one. you just need to add a new line into your scripts that add the "snap-in".

I'm in the knowledge that you're using exchange 2016, but you can just load the pssnapin for exchange 2010, (that usually is the one I use on all my scripts because it's always present from version 2010 on).

So the solution would be quite simple:
1. Open your script "E:\Scripts\Get-mailboxReport.ps1" and add this line at the beginning :
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;

or you can use any of this one:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;


And about the task manager part, I did a TechNet article that you should be interested in here:
You can install exchange management tools on the workstation you'rescheduling the report on  and add at the start of the script "add-psssnapin *exchange*".

Or just schedule it on exchange server ,depending on the workload it has
Tech ManInformation TechnologyAuthor Commented:
Let me try this solutions, I will let you know which worked the best.

Thank you
Tech ManInformation TechnologyAuthor Commented:
I read all the articles and tried to come up with solution but it is not working for me.
This is what I have:
I included necessary cmdlet to my powershell to call Exchange PSSnapin.
when I run this directly from powershell it works:

.\Get-MailboxReport.ps1 -All -SendEmail -MailTo -MailFrom -MailServer smtp.domain.local

this is how I set up in my Task scheduler under Action section :

Add argument (optional): -Command "& 'E:\Scripts\Get-MailboxReport.ps1' -All -SendEmail -MailTo -MailFrom -MailServer smtp.domain.local"

Start in (optional): E:\Scripts

when I run this task, task is running and running and nothing happens. I have to end the task manually.

Please let me know your thoughts.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.