How to schedule Exchange PowerShell Script using Windows Task Scheduler

Hi, so you have downloaded one of my Exchange PowerShell script, and you liked it ( i hope) , so now you want to schedule the script to run in a schedule that you like.

SO EASY 🙂 Follow my steps :

1. Open the Task Scheduler in Windows > Task Scheduler Library >right click and “Create Basic Task..

Schedule_PowerShell_Task_Scheduler_!2112

2. Type a descriptive name for your task and click Next.

3. On the Trigger section, choose your schedule that fits your needs.

4. On the Action, choose “Start a program” ,Click Next.

5. 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

Schedule_PowerShell_Task_Scheduler_!323

 

Be careful !!!!!!!!!!

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 instead of D:\Myscript.PS1, to have 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.

 

10 comments on “How to schedule Exchange PowerShell Script using Windows Task Scheduler

  1. 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]

  2. 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]

  3. Pingback: PowerShell Script Scheduling using Task Scheduler Trcik | Ammar Hasayen - Blog

  4. 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 Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; ” c:\myscript.ps1

  5. 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 Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; “D:\scripts\Get-CorpEmailReport.ps1 -ScriptFilesPath C:\temp\MonthlyExchReport -SendMail:$true -MailFrom MonthlyExchReport@XXX.com -MailTo ITApps@XXX.com -MailServer XXX

    and also without the -NonInteractive switch

    -command”.’C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; ” D:\scripts\Get-CorpEmailReport.ps1 -ScriptFilesPath C:\temp\MonthlyExchReport -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

      • 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s