# Cleanup logs older than the set of days in numbers $days = 30 # Path of the logs that you like to cleanup $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 -Debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder if (Test-Path $TargetFolder) { $Now = Get-Date $LastWrite = $Now.AddDays(-$days) $Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $_.Name -like "*.log" -or $_.Name -like "*.blg" -or $_.Name -like "*.etl" } | Where-Object { $_.lastWriteTime -le "$lastwrite" } | Select-Object FullName foreach ($File in $Files) { $FullFileName = $File.FullName Write-Host "Deleting file $FullFileName" -ForegroundColor "yellow"; Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null } } Else { Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red" } } If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path),"-ConfFile ",('"{0}"' -f $ConfFile) -Verb RunAs }Else{ Write-Verbose "I AM (Ps>)ROOT" #gci ‘C:\Program Files\Microsoft\Exchange Server\V15\Logging’ -Directory | gci -Include ‘*.log’,’*.blg’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-7) | Remove-Item -Recurse -Force -Confirm:$false #gci ‘C:\Inetpub\logs’ -Directory | gci -Include ‘*.log’,’*.blg’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-14) | Remove-Item -Recurse -Force -Confirm:$false CleanLogfiles($IISLogPath) CleanLogfiles($ExchangeLoggingPath) CleanLogfiles($ETLLoggingPath) CleanLogfiles($ETLLoggingPath2) }