Skip to content

Instantly share code, notes, and snippets.

@rickupton
Last active August 9, 2024 06:57
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
@alessoingross
Copy link

You want to compact all PST files you can use the manual method to reduce your PST file size. Also, try the manual method and compact the Outlook PST file simply open the MS Outlook and follow the steps. However manual method is a lengthy process and time-wasting. I suggest the best way the Toolsbaer Outlook PST Compact Tool, you can compact all PST files. This tool frees up valuable storage space while keeping your Outlook data intact. This tool works smoothly when it comes to compressing your email data, attachments, and other important data. This application compacts PST files in just a few clicks and improves MS Outlook performance as well as prevents large PST files from corruption.

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