Introduction
Managing Outlook‘s offline storage table (OST) files is an important aspect of email system maintenance. These files can grow large over time and consume valuable disk space, especially on systems with limited storage. This PowerShell script automatically removes OST files that haven’t been accessed in a specified period, helping to maintain system performance and storage efficiency.
The Script
<#
taskinfo:
- This script is used to remove OST files older than specified days from user profile
#>
$PeriodDays = 60
$OstPath = $Env:LocalAppData + "\Microsoft" + "\Outlook"
$ost = Get-ChildItem $OstPath | Where-Object { $_.Extension -eq ".ost"}
foreach($f in $ost) {
$LastAccessTime = $f.LastAccessTime
$CurrentDate = Get-Date
$Tim = New-TimeSpan $LastAccessTime $CurrentDate
$Days = $Tim.Days
if ($Days -gt $PeriodDays)
{
$f
$f | Remove-Item -Force
}
}
How It Works
This PowerShell script performs automated maintenance of Outlook OST files through several steps:
- Configuration: Sets a customizable retention period (default 60 days)
- Location Detection: Automatically locates the Outlook OST files in the user’s profile
- Age Calculation: Determines the last access time of each OST file
- Cleanup Process: Removes files that exceed the specified age threshold
- Forced Removal: Uses the -Force parameter to ensure successful deletion
Conclusion
This script provides an efficient solution for maintaining Outlook storage health by automatically removing outdated OST files. It’s particularly useful for system administrators managing multiple workstations or users with limited storage space. The script can be easily integrated into regular maintenance routines or scheduled tasks for automated execution.
Remember to adjust the retention period according to your organization‘s needs and ensure users are aware of the cleanup policy to prevent any unexpected data access issues.