$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)