Skip to content

Instantly share code, notes, and snippets.

@rickupton
Created November 1, 2015 21:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rickupton/8b1cc2c94cfeaaa68a65 to your computer and use it in GitHub Desktop.
Save rickupton/8b1cc2c94cfeaaa68a65 to your computer and use it in GitHub Desktop.
Compact and backup all Microsoft Outlook PST data files using a 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment