Schedule PowerShell Exchange script
Sometimes you might need to schedule PowerShell Exchange script using the Windows Task Scheduler. In this post, I will walk you through how to perform this task. You need to make sure you have Exchange PowerShell installed on the machine where the script will be run. If you are an Exchange administrator, then you should browse the internet for Exchange script, as there are many amazing scripts out there that can help you better manager and monitor your Exchange installation.
- Open the Task Scheduler in Windows > Task Scheduler Library > right click and “Create Basic Task..“
- Type a descriptive name for your task and click Next.
- On the Trigger section, choose your schedule that fits your needs.
- On the Action, choose “Start a program“, Click Next.
- On the “Start a Program” subsection, Enter:
- In the Program/Script, type C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- In the Add Arguments (Optional) ,type
-command ". 'c:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; D:\Myscript.PS1
- In the Start in (Optional), leave it empty
Note: Replace D:\Myscript.PS1 with your script path.
Note: Make sure the machine has Exchange PowerShell Management Tools installed already.
Final Thoughts
I hope by now you know how to schedule PowerShell Exchange script using the Windows Task Manager. As you can see, it is an easy thing to do. I will also leave you with a quick top when doing so.
I have discovered in a hard way, that you will get problem, and the script will not run, if the path length to your script is too long. For example, it is not good idea to host your script under deep folder structure like D:\Path1\path2\path3\path3 management scripts for Exchange that i love to schedule\Script Directory\Cool Scripts\Myscript.PS1
This is a long path, and there is a limit for the length of commands in the Windows Task Scheduler. Keep that in mind.
Hi dear,
I liked this report so much ☺ unfortunately I still can’t schedule it, I have added everything as you mentioned but it’s still run on windows power shell not EMS
Your kind help will be highly appreciated
[cid:image001.png@01CEC01D.4D617420]
Omar Ammar
Senior IT Officer
Information Technology
Airport International Group
P.O. Box 3717 Amman 11821
The Hashemite Kingdom of Jordan
Tel: +962 (6) 445 3000
Fax: +962 (6) 445 3444
Mobile +962 (79) 519 4994
[Description: Description: imagesCAYRDJ9A]
[Description: untitled]
can you see the attached image i have sent
nope. please email me on me@ammarhasayen.com
Dear Its working☺☺☺ I just changed the directory ☺ many thanks dear
Regards
Omar Ammar
From: Omar ammar
Sent: Thursday, October 3, 2013 9:46 AM
To: ‘Ammar Hasayen – Blog’
Subject: RE: [New post] How to schedule Exchange PowerShell Script using Windows Task Manager
Hi dear,
I liked this report so much ☺ unfortunately I still can’t schedule it, I have added everything as you mentioned but it’s still run on windows power shell not EMS
Your kind help will be highly appreciated
[cid:image001.png@01CEC020.C31012A0]
Omar Ammar
Senior IT Officer
Information Technology
Airport International Group
P.O. Box 3717 Amman 11821
The Hashemite Kingdom of Jordan
Tel: +962 (6) 445 3000
Fax: +962 (6) 445 3444
Mobile +962 (79) 519 4994
[Description: Description: imagesCAYRDJ9A]
[Description: untitled]
Great ! Happy it works for you
You are missing a quote and the quotes need to be retyped. Otherwise this works. Keep in mind the path may change, depending on the version of Exchange. I suggest you pull it from the PS shortcut. Here is what worked for me:
-command “. ‘c:Program FilesMicrosoftExchange ServerV15binRemoteExchange.ps1′; Connect-ExchangeServer -auto; ” c:myscript.ps1
Hello Ammar,
The script (Get-CorpEmailReport) works like a charm but i am unable to add this to run thru windows task scheduler on Win Server 2012 R2. I have tried minimizing the folder structure (for both : the path where the PS1 file is available and the ScriptLogPath directory)
this is what i am putting as an argument below:
-NonInteractive -WindowStyle Hidden -command “. ‘C:Program FilesMicrosoftExchange ServerV15binRemoteExchange.ps1’; Connect-ExchangeServer -auto; “D:scriptsGet-CorpEmailReport.ps1 -ScriptFilesPath C:tempMonthlyExchReport -SendMail:$true -MailFrom MonthlyExchReport@XXX.com -MailTo ITApps@XXX.com -MailServer XXX
and also without the -NonInteractive switch
-command”.’C:Program FilesMicrosoftExchange ServerV15binRemoteExchange.ps1′; Connect-ExchangeServer -auto; ” D:scriptsGet-CorpEmailReport.ps1 -ScriptFilesPath C:tempMonthlyExchReport -SendMail:$true -MailFrom MonthlyExchReport@XXX.com -MailTo ITApps@XXX.com -MailServer XXX
The goal is to set it to run last friday of each month and is sent to an email (shared mailbox)
Please help
Hassan Abidi
Hi 🙂 you should put all this in batch file and then run the batch file from task schedular. I will write detailed blog about this as i got similar comments
Hello Ammar, thanks for a prompt response. I however, another issue that i am dealing with and would need your input on how it can be done:
Task: There are some custom holidays that i need to be imported to all mailboxes in the organization. The custom holiday event must be set to all day events and it has to be with status “Out of Office” not “busy/Free/Tentative etc” We do not want to have any kind of user interaction, meaning no .hol file to be created and then sent it via email OR via GPO.
Is there any way to create these events and the status via script and add these events automatically to all mailboxes (default calendar) in the organization
Please advise!
Thanks