Here's a way to automatically compact and backup all of your Microsoft Outlook PST data files on your Windows PC using only free software, AutoIT and PowerShell. AutoIT is freeware, and PowerShell is included in Microsoft Windows. Compacting your PST files before you back them up can reduce the amount of disk space required both at the source and destination locations of your backup.
So, to get started:
- Get the free AutoIT Full Installation and AutoIT Script Editor from the AutoIT Downloads page.
- Ensure PowerShell scripts that you create can run on your computer.
- Create an AutoIT script that, when the Mail control panel is open, will compact all PST files and then close the Mail control panel.
- Create a Powershell script that opens the Mail control panel and then executes the AutoIT script.
Here below are the AutoIT and Powershell scripts that I am running on my Windows 8.1 PC with Outlook 2013. I have a Windows Shortcut to my PowerShell script so that every time I log into my PC, my Outlook PST data files are compacted and backed up before Outlook is opened. I use the PowerShell script to also open other applications so that I may work on them while the Outlook PST data files are being backed up. Please feel free to copy the scripts below and modify them to work for you.
I am using Synology's Cloud Station to keep most of the data files on my laptop in sync with a Synology NAS, but Cloud Station doesn't work well with Outlook PST data files, which is why Cloud Station is configured by default to exclude the synchronization of files with a PST extension.
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 |
PowerShell script
# Rick Startup.ps1 | |
# Rick Upton, November 1, 2015 | |
# http://www.rickupton.com | http://gb.rickupton.com | |
# | |
# This script: | |
# 1) Compacts Microsoft Outlook personal storage table (PST) data files. | |
# 2) Backs up PST data files into a date and time stamped folder. | |
# 3) Launches multiple applications. | |
# | |
# To set this script up to launch every time you log into Windows: | |
# 1) Create a Windows shortcut for this script with a target like the following (not including the # sign): | |
# powershell.exe -command "& 'C:\Users\Rick\Documents\CloudStation\Rick Startup.ps1' | |
# 2) In "Run", type "shell:startup" to open the startup folder with shortcuts for apps which should open after logging into Windows. | |
# 3) Put the newly created shortcut for this script into the startup folder. | |
# 4) Are there any other shortcuts in the startup folder? Consider deleting the other shortcuts and having this script open those | |
# application instead to avoid a situation where the launch of other apps could interfere with the execution of the AutoIT3 (au3) | |
# script. To do this, copy the target for each shortcut in the startup folder and paste it into this script preceded by a & character | |
# so that this script executes the shortcut, then delete the shortcut from the startup folder. | |
# | |
# Potential future enhancements for this Powershell script: | |
# 1) I should rarely need to monitor disk space usage on the destination drive since I am backing up files to a Synology NAS that has | |
# a huge amount of free disk space. However, if disk space usage becomes a problem, then I could enhance this script to do one of the | |
# following: | |
# a) Delete backup files older than X days. | |
# b) Delete files with a certain day of the month in the backup name so that a sampling of old and new backup files are retained. | |
# c) Do one of the above only when disk space is less than X GB available. | |
# 2) If I get into a situation where I am away from home with my laptop for multiple days, then I could enhance the script to back up | |
# files to a USB drive when the NAS drive isn't detected. | |
# Open Mail control panel. | |
Show-ControlPanelItem Mail* | |
# Call AutoIT script to compact all Outlook PST files, waiting for the script to complete before moving on to the next step | |
# in this PowerShell script. The code for the AutoIT script is here: https://gist.github.com/rickupton/16f9a7b445a668c70c61 | |
Start-Process "C:\Users\Rick\Documents\CloudStation\Compact all PST files.au3" -Wait | |
# Launch multiple applications. | |
# * AFTER au3 script runs so that AutoIT3 (au3) script doesn't run into problems due to other apps being open. | |
# * BEFORE the lengthy copying of Outlook PST files begins so that other apps may be used during the PST file copy process. | |
# Open Chrome with multiple tabs using instructions here: http://www.laptopmag.com/articles/open-multiple-tabs-on-start-up-with-chrome | |
# I open Google Calendar, Weather.com's hourly forecast for San Jose, and my web mail's Spam folder so I can check these while waiting | |
# for Outlook to open (my Spam folder contents don't get downloaded to Outlook so I need to check it on the web). | |
& "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" | |
# Open other apps. | |
& "C:\Program Files\Microsoft Office 15\root\office15\ONENOTEM.EXE" /tsr | |
& "C:\Program Files (x86)\Synology\CloudStation\bin\launcher.exe" | |
# Check to see if the computer is connected to the F: drive thanks to modified version of this script: | |
# http://pshscripts.blogspot.com/2010/07/get-driveinfops1.html | |
# If the F: drive is ready, then backup the Outlook PST files. If the F: drive is not ready, then that's OK, because | |
# it probably means I'm not at home attached to the NAS. | |
$d = New-Object -typename System.IO.DriveInfo -argumentlist "F:" | |
"Drive {0}" -f $d.Name | |
" File type: {0}" -f $d.DriveType | |
if ($d.IsReady) { | |
" Volume label: {0}" -f $d.VolumeLabel | |
" File system: {0}" -f $d.DriveFormat | |
$fs = $d.AvailableFreeSpace/1gb | |
$tfs = $d.TotalFreeSpace/1gb | |
$TS = $d.TotalSize/1gb | |
" Available space to current user:{0, 15:n2} gb" -f $fs | |
" Total available space: {0, 15:n2} gb" -f $tfs | |
" Total size of drive: {0, 15:n3} gb" -f $ts | |
$Now = Get-Date -uformat "%Y-%m-%d %Hh%M" | |
$Source = "C:\Users\Rick\Documents\Rick Outlook Files\" | |
$Destination = "F:\Rick Outlook Files Backups\" | |
# Create folder with date as the folder name inside the destination folder | |
New-Item -ItemType directory -Path "$Destination$Now" | |
# Copy Outlook PST data files | |
$Files = Get-Childitem $Source -Recurse | |
foreach ($File in $Files) { | |
if ($File -ne $NULL) { | |
write-host "Copying $File" -ForegroundColor "Green" | |
write-host "from $Source" -ForegroundColor "Green" | |
write-host "to $Destination$Now" -ForegroundColor "Green" | |
write-host "-----" | |
Copy-Item $File.FullName -destination $Destination$Now -force | out-null | |
} | |
else | |
{ | |
Write-Host "No more files in $Source to copy." -foregroundcolor "DarkRed" | |
} | |
} | |
} | |
# Launch Microsoft Outlook | |
& "C:\Program Files\Microsoft Office 15\root\office15\OUTLOOK.EXE" |
2 comments:
Thank you for your post and for the script! I will use it maybe some day. I was looking for this solution because I moved my mail from one computer to another. Outside the express 6 on both machines. I imported the address book, incoming messages were pulled from the server (stored there). Than I found visit website to learn more horde open transfer and figured out how to do it corectly. From the created file, you can get data using any version of Outlook, using the import or creating an item. All is simply the best!
We use G Suite, the Control Panel Item Mail label is different:
Mail Setup - email@domain.com - G Suite
It worked like a charm for us after editing these lines:
15: WinWait("Mail Setup", "Data Files")
16: ControlClick("Mail Setup", "Data Files", "Button2")
48: WinClose("Mail Setup")
Thank you
Post a Comment