Skip to content

Instantly share code, notes, and snippets.

@MaxRei-dev
Last active November 11, 2021 08:00
Show Gist options
  • Save MaxRei-dev/5d1d12530c06f7b24f94f449b101918e to your computer and use it in GitHub Desktop.
Save MaxRei-dev/5d1d12530c06f7b24f94f449b101918e to your computer and use it in GitHub Desktop.
Clean Exchange Logs
$executionPolicy = Get-ExecutionPolicy
if ($executionPolicy -ne 'RemoteSigned') {
Set-Executionpolicy RemoteSigned -Force
}
$days = 2
$IISLogPath = "C:\inetpub\logs\LogFiles\"
$ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
$ETLLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2 = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs"
Function CleanLogfiles($TargetFolder)
{
Write-Host -ForegroundColor Yellow -BackgroundColor Black $TargetFolder
if (Test-Path $TargetFolder) {
$Now = Get-Date
$LastWrite = $Now.AddDays(-$days)
$Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $_.Extension -in '.log', '.blg', '.etl' -and $_.LastWriteTime -le $lastwrite } | Select-Object -ExpandProperty FullName
foreach ($File in $Files)
{
Write-Host "Deleting file $File" -ForegroundColor "yellow";
try {
Remove-Item $File -ErrorAction Stop
}
catch {
Write-Warning -Message $_.Exception.Message
}
}
}
else {
Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red"
}
}
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment