Closing Outlook via a (scheduled) script

Unlike being able to start Outlook via a a scheduled task, the Task Scheduler in Windows sadly doesn’t have a “close application” option. However, you can use a simple vbs-script for it to close Outlook gracefully or even kill the process (as a failsafe method).

By using a script to close Outlook, you can for instance make sure that Outlook is closed when a(n automated) backup is being made so that the pst-files are no longer locked and the backup process doesn’t fail.

The script examples in this guide can either be ran as a standalone vbs-script, be scheduled or be part of a larger script such as logon or logoff scripts.


Script 1: Close Outlook gracefully

This method of closing Outlook would be similar to closing Outlook via File-> Close or by pressing the Close button ( X ) in the top-right corner of the Outlook window;

Set objOutlook = CreateObject("Outlook.Application")
objOutlook.Quit

To use this code, place it in a text-file and save it as closeoutlook.vbs or download this zip-file; closeoutlookscripts.zip

Note: Closing Outlook in this way could fail if you get an exit prompt such as “Do you want to save changes to…” when you have an edited unsaved item open.

Script 2: Close Outlook gracefully with verification

This method of closing Outlook is the same as the Script 1 method but improves upon it by checking first if Outlook is indeed running. The downside of the bare minimum script above is that it assumes Outlook is running and that it will first launch Outlook if it isn’t running at the moment the script is triggered. The code example below will first check if the outlook.exe process exists and only executes the closing script if this indeed is the case.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Outlook.exe'")
For Each objProcess in colProcessList
   Set objOutlook = CreateObject("Outlook.Application")
   objOutlook.Quit
Next

To use this code, place it in a text-file and save it as closeoutlookverify.vbs or download this zip-file; closeoutlookscripts.zip

Note: Closing Outlook in this way could fail if you get an exit prompt such as “Do you want to save changes to…” when you have an edited unsaved item open.

Script 3: Kill Outlook

This method of closing Outlook would be similar to ending the outlook.exe process via Task Manager. This script can be used when Outlook is non-responsive or when the graceful method fails and you need be be sure that Outlook is closed.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Outlook.exe'")
For Each objProcess in colProcessList
   objProcess.Terminate()
Next

To use this code, place it in a text-file and save it as killoutlook.vbs or download this zip-file; closeoutlookscripts.zip

Note: Closing Outlook in this way could result in Outlook informing you that Outlook wasn’t closed properly the next time you start it and will do a background scan on the integrity of the pst-file or a manual scan is required via scanpst.exe. Therefore, use this script with caution and only when needed.

Combining the scripts

To use the best of both scripts, you could combine the “Close Outlook” and the “Kill Outlook” scripts in a single vbs-script file where you’d be placing the “Close Outlook” script before the “Kill Outlook” script. To make sure you provide Outlook with some time to close gracefully, you can place a sleep-command between the 2 scripts like this.

WScript.Sleep 30000

This will make the script wait for 30 seconds (30000 milliseconds) before it continues. In other words, if Outlook is still running after 30 seconds, it is being killed.

An example is included in the closeoutlookscripts.zip as closeoutlookfailsafe.vbs and closeoutlookverifyfailsafe.vbs.

Note: Since the closing time of Outlook can vary based on your system, closing Outlook gracefully could take longer. Therefore, time how long it will take before Outlook is fully closed on your system, add a good 15-20 seconds to that and set that as the sleeping time (in milliseconds!). In any case, I would not recommend setting it lower than 30 seconds.


Creating a Scheduled Task to close Outlook

If you want to use this script as a Scheduled Task, for instance 2 minutes before your scheduled backup task, you can do this in the following way;

Windows XP

  1. Open the Scheduled Tasks applet from the Windows Control Panel.
    If your Control Panel is in Category View, you’ll find it under Performance and Maintenance.
  2. Double click on “Add Scheduled Task”.
  3. Button Next.
  4. Click on Browse…
  5. Browse to the location where you’ve stored one of the above vbs-scripts.
  6. Select the script and press Open.
  7. Type a name for the task.
  8. Select the schedule for the task to be performed.
  9. Click Next.
  10. Select the time to perform the task and, depending on your selected schedule, further scheduling refinements.
  11. Provide your username and password.
  12. Click Next.
  13. Click Finish.

Windows Vista and Windows 7

  1. Open the Schedule Tasks applet from the Windows Control Panel.
    You can find it at the bottom under “System and Maintenance” in Windows Vista or “System and Security” in Windows 7. Another way to find it is to simply type “schedule tasks” in the Search box at the top right in Control Panel.
  2. From the Action pane on the right side of the Task Scheduler window click;
    Create Basic Task…
  3. Provide a name for the task and optionally a description.
  4. Click Next.
  5. Select the schedule for the task to be performed.
  6. Click Next.
  7. Select the time to perform the task and, depending on your selected schedule, further scheduling refinements.
  8. Click Next.
  9. Select: Start a program
  10. Click Browse…
  11. Browse to the location where you’ve stored one of the above vbs-scripts.
  12. Select the script and press Open.
  13. Click Next.
  14. Click Finish.

Deleting the scheduled task

If you ever want to delete the task, you can do so by opening Task Scheduler again.

  • Windows XP
    Simply select the task and press Delete.
  • Windows Vista and Windows 7
    In the Tasks Scheduler, on the left side, select the Task Scheduler Library and you’ll find the task back in the center pane at the top. Right click the task and choose “Delete”.

Closing Outlook via a Scheduled Task can prevent failed automated backups of your pst-files. (Click the image to enlarge)
Closing Outlook via a Scheduled Task can prevent failed automated backups of your pst-files.
(Click the image to enlarge)