$days=7 $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" $TransportLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs" Function CleanLogfiles($TargetFolder) { if (Test-Path $TargetFolder) { $Now = Get-Date $LastWrite = $Now.AddDays(-$days) $Files = Get-ChildItem $TargetFolder -Include *.log,*.blg, *.etl, *.txt -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} foreach ($File in $Files) {Write-Host "Deleting file $File" -ForegroundColor "white"; Remove-Item $File -ErrorAction SilentlyContinue | out-null} } Else { Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "white" } } CleanLogfiles($IISLogPath) CleanLogfiles($ExchangeLoggingPath) CleanLogfiles($ETLLoggingPath) CleanLogfiles($ETLLoggingPath2) CleanLogfiles($TransportLoggingPath)