Skip to content

Instantly share code, notes, and snippets.

@rickupton
Last active September 15, 2025 09:26
Show Gist options
  • Save rickupton/16f9a7b445a668c70c61 to your computer and use it in GitHub Desktop.
Save rickupton/16f9a7b445a668c70c61 to your computer and use it in GitHub Desktop.
Automatically compact all Microsoft Outlook PST data files using an AutoIT script
;//Compact all PST files.au3
;//Rick Upton, November 1, 2015
;//http://www.rickupton.com | http://gb.rickupton.com
;//
;//This AutoIT script is a slightly modified version of code posted here: http://superuser.com/questions/836758/bulk-compaction-of-700gb-of-pst-files
;//The purpose of this code is to automatically compact all Outlook PST files on a Windows PC.
;//
;//Using a PowerShell script, open the Mail control panel and then run this script using the following two commands:
;//Show-ControlPanelItem Mail*
;//Start-Process "C:\Compact all PST files.au3" -Wait
;//
#include <GUIListView.au3>
SplashTextOn("Automated compaction of Outlook files in process", "DO NOT TOUCH THE COMPUTER WHILE THIS IS RUNNING.", 400, 80)
Opt("WinTitleMatchMode", 4)
WinWait("Mail Setup - Outlook", "Setup e-mail accounts and dire")
ControlClick("Mail Setup - Outlook", "Setup e-mail accounts and dire", "Button2")
WinWait("Account Settings", "AcctMgr Tab")
;//Gets list of Data files listed
$sTitle = "Account Settings"
$hWnd = WinGetHandle($sTitle)
If @error Then
MsgBox(0, "Error", "Unable to find window")
EndIf
WinActivate($hWnd)
$hlist = ControlGetHandle($hWnd, "", "[CLASS:SysListView32; INSTANCE:2]")
If @error Then Exit
$arraycount = _GUICtrlListView_GetItemCount($hlist)
Local $ltext[$arraycount]
$i = 0
Do
$ltext[$i] = _GUICtrlListView_GetItemText($hlist, $i)
$i = $i + 1
Until $i = $arraycount
;//Goes into each listed Data file and compacts them
$b = 0
Do
_GUICtrlListView_ClickItem($hlist, $b, "left", False, 2)
Sleep(1000)
WinWaitActive("Outlook Data File")
ControlClick("Outlook Data File", "", "[CLASS:Button; INSTANCE:2]") ; click Compact Now
Sleep(1200)
If WinExists("Compact Now") Then WinWaitClose("Compact Now")
WinClose("Outlook Data File")
$b = $b + 1
Until $b = $arraycount
WinClose("Outlook Data Files")
WinClose("Account Settings")
WinClose("Mail Setup - Outlook")
SplashOff()
Exit
@Pauls83
Copy link

Pauls83 commented Oct 29, 2020

Does this work the same way as pressing the compact now button in outlook?

@rickupton
Copy link
Author

Does this work the same way as pressing the compact now button in outlook?

Yes, this script automates the process of pressing "Compact Now" for each data file.

@sanjay21065
Copy link

To compact a PST file, you can use both manual and automatic methods depending on your needs. Manually, you can reduce the PST file size in Outlook using three options: the “Compact Now” feature, the Archive function, or by deleting unwanted emails and emptying the Deleted Items folder. The "Compact Now" option is found under Account Settings > Data Files > Settings, where you simply click Compact Now to reduce file size. Archiving old items is another way—just go to File > Info > Tools > Clean Up Old Items, and move older emails to an archive.
Read more: Manual and Automatic Methods to Compress PST File

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment