Script to gracefully close an Outlook .pst file so it can be backed up

I was listening to the latest episode of the Casting from the Server Room podcast this morning, and one of the discussions was about backing up an Outlook .pst when users leave Outlook open overnight, which is when the backup is scheduled to run. 

In order to back an Outlook .pst file, the .pst file must not be in use.  The easiest way to make sure the .pst file isn’t in use is to close Outlook on the computer the .pst resides on.

I found this .vbs script, written by Bill Stewart, on Chris’s blog that will close Outlook gracefully. 

On Error Resume Next
Set Outlook = GetObject(, “Outlook.Application”)
If Err = 0 Then
Outlook.Quit()
End If

Just killing Outlook can result in .pst corruption, so I don’t suggest using a program like pskill to terminate outlook.exe.

Bill also has an Exchange version of the above script, which can be downloaded here

You can schedule these scripts to run nightly with a Windows Scheduled Task, or with a program like psexec, which will execute programs on remote machines through a command line or script.

Comments [6]

  1. How do you get around the “Delete Deleted” with this script? If the confirm empty the trash upon existing is set, it will hang prompting you Yes or No. Any way to put the answer in the script?

  2. //Copy and paste (jscript)

    if(IsProcessRunning(“OUTLOOK.EXE”))
    {
    WScript.Echo(“Outlook is running. Closing it…”);
    CloseOutlook();

    if(IsProcessRunning(“OUTLOOK.EXE”))
    {
    WScript.Echo(“Outlook is still running. Will wait for 10s…”);
    WScript.Sleep(10000);
    if(IsProcessRunning(“OUTLOOK.EXE”))
    {
    WScript.Echo(“Killing outlook forcefully!”);
    KillProcess(“OUTLOOK.EXE”);
    }
    else
    {
    WScript.Echo(“Outlook died in 10s.”);
    }
    }
    else
    {
    WScript.Echo(“Outlook has been closed.”);
    }

    }
    else
    {
    WScript.Echo(“Outlook is not running.”);
    }

    WScript.Sleep(2000);

    function CloseOutlook()
    {
    var objOutlook = new ActiveXObject(“Outlook.Application”);
    var lngCount = objOutlook.Inspectors.Count;
    for(var i = lngCount; i>0; i–)
    {
    objOutlook.Inspectors.Item(i).CurrentItem.Save();
    objOutlook.Inspectors.Item(i).Close(true);
    }

    objOutlook.quit();
    }

    function KillProcess(ImageName)
    {
    var WshShell = new ActiveXObject(“WScript.Shell”);
    WshShell.Run(“taskkill /T /F /IM ” + ImageName, 7, true);
    }

    function IsProcessRunning(ImageName)
    {
    var WshShell = new ActiveXObject(“WScript.Shell”);
    var output = “”;

    var oExec = WshShell.Exec(“tasklist /FO CSV /NH /FI \”IMAGENAME eq ” + ImageName + “\””);

    while(oExec.Status == 0) WScript.Sleep(100);

    while( ! oExec.StdOut.AtEndOfStream) output += oExec.StdOut.Read(1);

    return output != “”;
    }

  3. On Error Resume Next
    Set Outlook = GetObject(, “Outlook.Application”)
    If Err = 0 Then
    Outlook.Quit()
    End If

    doesn’t seem to work for me… anyone else getting good/bad results?

  4. 0x80040600,

    Try changing doublequote signs “ to normal ”

    Worked for me

  5. In this situation advise try-pst files viewer,program helped me many times and as far as i know is freeware,it will help you to restore your data from files with *.pst and *.ost extension,tool will work under all supported versions of Microsoft Windows operating system, as well as with Microsoft Outlook,can retrieve all contents as a number of files in *.vcf, *.txt and *.eml formats,will extract thousands of different files, that will be placed into any folder, upon your choice,converting of recovered data into a *.pst file, that can be opened by any mail client or viewer .pst file, compatible with Microsoft Outlook, file size will not exceed 1Gb.

  6. In this situation try next software-ost unavailable when exchange is unavailable,as is known program is free,it was developed to help you with damaged files in *.ost format,program is based on effective *.ost files recovery technology, that allows easily convert them to *.pst files, that can be opened by any mail client, compatible with Microsoft Outlook,allows to restore emails, contacts, tasks and calendars, encrypted in *.ost file,recover Exchange 2007 ost and restore your mail from all folders and subfolders,allows to recover all files separately: *.eml, *.vcf and *.txt files will be extracted and placed in the same folder.

Leave a Reply

Your email address will not be published. Required fields are marked *