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 PowerShell or 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 PowerShell or vbs-script, be scheduled, or be part of a larger script such as logon or logoff scripts.
- How to use these examples
- PowerShell Script 1: Close Outlook gracefully
- PowerShell Script 2: Close Outlook gracefully with verification
- VBS Script 1: Close Outlook gracefully
- VBS Script 2: Close Outlook gracefully with verification
- Creating a Scheduled Task to close Outlook
Tip!
An example where this script is being combined with a backup script can be found in the guide: Script to schedule your pst-file back-up
How to use these examples
The example scripts in this guide can be added to your own scripts or run as standalone scripts.
To create a standalone script, place it in a text-file and save it as closeoutlook.ps1
when using a PowerShell script example or closeoutlook.vbs
when using a VBS script example.
As an alternative, you can download this zip-file which contains all the examples; closeoutlookscripts.zip
To quickly run a PowerShell script, right-click on the closeoutlook.ps1
file and choose; Run with PowerShell
To quickly run a vbs-script, simply double click on it.
For more info about using PowerShell see; Executing PowerShell Scripts FAQ and Tips & Tricks.
PowerShell script 1: Close Outlook gracefully
To properly close Outlook, you can use the following code to hook into the running Outlook session as a ComObject, so we can use the Outlook Object Model (OOM), and use the Quit method.
$olApp = New-Object -ComObject Outlook.Application $olApp.Quit() Remove-Variable olApp
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.
Note:
Any changes to items not already saved are discarded.
PowerShell script 2: Close Outlook gracefully with verification
The downside of Script 1 is that it assumes that Outlook is running and that it will first launch Outlook if it isn’t running at the moment the script is triggered.
To prevent this, we’ll use PowerShell to first look at the Windows processes with Get-Process and if Outlook is found, the method of Script 1 is being used to close Outlook.
This script also adds the following improvements;
- After the Close command has been sent, the script uses Sleep to wait 15 seconds for Outlook to close and then checks whether Outlook has indeed closed by using HasExited.
- If Outlook still hasn’t closed by then, the Outlook process is stopped with Stop-Process, which is similar to ending the outlook.exe process via Task Manager.
$outlook = Get-Process outlook -ErrorAction SilentlyContinue if ($outlook) { $olApp = New-Object -ComObject Outlook.Application $olApp.Quit() Remove-Variable olApp Sleep 15 if (!$outlook.HasExited) { $outlook | Stop-Process -Force } } Remove-Variable outlook
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 5-10 seconds to that and set that as the sleeping time (in seconds!). In any case, I would not recommend setting it lower than 15 seconds.
VBS-script 1: Close Outlook gracefully
In case you are still using vbs-scripts, you can use the following code to achieve the same as PowerShell Script 1.
Set objOutlook = CreateObject("Outlook.Application") objOutlook.Quit Set objOutlook = Nothing
VBS-script 2: Close Outlook gracefully with verification
To achieve the same as PowerShell Script 2 with a vbs-script, you can use the following code.
delay = 15000 'delay in milliseconds to let Outlook close gracefully strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 'If Outlook is running, let it quit on its own. For Each Process in objWMIService.InstancesOf("Win32_Process") If StrComp(Process.Name,"OUTLOOK.EXE",vbTextCompare) = 0 Then Set objOutlook = CreateObject("Outlook.Application") objOutlook.Quit Set objOutlook = Nothing WScript.Sleep delay Exit For End If Next 'Make sure Outlook is closed and otherwise force it. Set colProcessList = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = 'Outlook.exe'") For Each objProcess in colProcessList objProcess.Terminate() Next Set objWMIService = Nothing Set objOutlook = Nothing Set colProcessList = Nothing
Creating a Scheduled Task to close Outlook
Aside from including the above scripts within other scripts, you can also use this script as a Scheduled Task.
For instance, you could trigger this task 2 minutes before your scheduled backup task (in case you don’t do this via a dedicated task for Outlook).
You can schedule such a task in the following way;
- Open Task Scheduler.
Just type “Task Scheduler” directly after opening the Start Menu or within the Search box. - From the Actions pane on the right side choose: Create Basic Task…
- Specify a name for the task, and optionally a description, and press Next.
- Select the trigger for the task to run. In this case; Daily, Weekly or Monthly.
- Configure the recurrence pattern.
- For the action select: Start a program.
- Fill out the “Start a Program” form in the following way;
- Click Next to get an overview of your newly created Task and then click Finish to actually create it.
Deleting the scheduled task
If you ever want to delete the task, you can do so by opening Task Scheduler again.
In the Tasks Scheduler, on the left side, select the Task Scheduler Library and you’ll find the task back in the center. Right click the task and choose “Delete”.
Closing Outlook via a Scheduled Task can prevent failed automated backups of your pst-files.