Skip to content

Instantly share code, notes, and snippets.

@BretFisher
Created February 11, 2016 19:16
Show Gist options
  • Save BretFisher/68fe60ac4f0c94bc8086 to your computer and use it in GitHub Desktop.
Save BretFisher/68fe60ac4f0c94bc8086 to your computer and use it in GitHub Desktop.
Exchange 2013 Log Cleanup PowerShell Script
Set-Executionpolicy RemoteSigned
$days=10 #You can change the number of days here
$IISLogPath="C:\inetpub\logs\"
$ExchangeLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
Function CleanLogfiles($TargetFolder)
{
if (Test-Path $TargetFolder) {
$Now = Get-Date
$LastWrite = $Now.AddDays(-$days)
$Files = Get-ChildItem $TargetFolder -Include *.log,*.blg -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{Write-Host "Deleting file $File" -ForegroundColor "Red"; Remove-Item $File -ErrorAction SilentlyContinue | out-null}
}
Else {
Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red"
}
}
# lets do this
Write-Host "Removing IIS and Exchange logs; keeping last" $days "days"
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment