Set up monitoring to ensure your cleanup script is working effectively and not causing issues.
Create a simple monitoring script to track disk space trends. Save as C:\Scripts\MonitorDiskSpace.ps1:
# Disk Space Monitoring Script
$LogFile = "C:\Scripts\DiskSpaceLog.txt"
$Volume = Get-Volume C
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$FreeSpaceGB = [math]::Round($Volume.SizeRemaining / 1GB, 2)
$UsedSpacePercent = [math]::Round((($Volume.Size - $Volume.SizeRemaining) / $Volume.Size) * 100, 2)
$LogEntry = "$Timestamp - Free Space: $FreeSpaceGB GB ($UsedSpacePercent% used)"
Add-Content -Path $LogFile -Value $LogEntry
# Alert if disk usage exceeds 85%
if ($UsedSpacePercent -gt 85) {
Write-EventLog -LogName Application -Source "Exchange Cleanup" -EventId 1001 -EntryType Warning -Message "Disk usage is $UsedSpacePercent% - cleanup may need adjustment"
}
Check the Windows Event Log for cleanup activities:
Get-WinEvent -FilterHashtable @{LogName='Application'; StartTime=(Get-Date).AddDays(-1)} | Where-Object {$_.LevelDisplayName -eq 'Information' -and $_.Message -like '*Exchange*'}
Review the scheduled task history:
Get-ScheduledTask -TaskName "Exchange Log Cleanup" | Get-ScheduledTaskInfo
Verification: Check that log files are being cleaned up regularly:
# Check oldest files in log directories
$LogPaths = @("$env:SystemDrive\inetpub\logs\LogFiles", "$env:ExchangeInstallPath\Logging")
foreach ($Path in $LogPaths) {
if (Test-Path $Path) {
$OldestFile = Get-ChildItem -Path $Path -Recurse -File | Sort-Object LastWriteTime | Select-Object -First 1
Write-Host "Oldest file in $Path`: $($OldestFile.LastWriteTime) - $($OldestFile.Name)"
}
}
Warning: If you notice Exchange services becoming unstable after cleanup, check that you haven't deleted active transaction logs. Transaction logs should be truncated through proper backup procedures, not file deletion.