Skip to content

Instantly share code, notes, and snippets.

@Hardzen
Forked from RippieUK/CleanExchangeLogs.ps1
Last active July 23, 2020 09:35
Show Gist options
  • Save Hardzen/89cb5efed278650ba0df04778b73961a to your computer and use it in GitHub Desktop.
Save Hardzen/89cb5efed278650ba0df04778b73961a to your computer and use it in GitHub Desktop.
## Copied and cleaned up the script from https://gallery.technet.microsoft.com/Clear-Exchange-2013-Log-71abba44
## Tested on Exchange 2016
$executionPolicy = Get-ExecutionPolicy
if ($executionPolicy -ne 'RemoteSigned') {
Set-Executionpolicy RemoteSigned -Force
}
$days = 7
$IISLogPath=((Get-WebConfigurationProperty '/system.applicationHost/sites/siteDefaults' -Name 'logfile.directory').Value).Replace("%SystemDrive%",$env:SystemDrive)
$ExchangeLoggingPath=$env:exchangeinstallpath + "Logging\"
$ETLLoggingPath=$env:exchangeinstallpath + "Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2=$env:exchangeinstallpath + "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