Exchange Clean Logs ETL 2013/2016/2019 with detailed logging

The estimated reading time 4 minutes

In old fashoined exchange onpremises installations, lot of people face an issue exchange does not accept new mails, because of lack of ressources. So how to cleanup the drive where exchange is installed?

Some days ago the good old technet gallery offered a small script to manage this task. But now the technet gallery (R.I.P) was disabled. You will find a lot sites, where you can download the cleanup script, but you don’t have any logging implemented in this script. I just wanted to know if the script deletes these unwanted logs or just does nothing. Therefore I created a small logging function and also a cleanup for this logs 😉

First of all the script from technet gallery you may know:

Set-Executionpolicy RemoteSigned
$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\"


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 -Include *.log,*.blg, *.etl -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
        $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"
    }
}

CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)

Here is my modiefied script part which loggs the removing and also removes these log after the period of time you can define.(also the variable “$days” is used).

#logging section
$logpathfolder = "C:\logging"
[string]$date = Get-Date -Format "yyyy-MM-dd-HH-mm"
#protcol starting the script (never deleted)
"Script run on $date" | Out-File -FilePath "$logpathfolder\excleanup.txt" -Append
 
Start-Transcript -Path "$logpathfolder\$date-excleanup.log"
 
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)
 
Stop-Transcript
  
### removing old .log files 
$fileextension = "log"
Write-Host "removing logfiles older than $days days from logpath $logpathfolder "
$limit = (Get-Date).AddDays(-$days)
$logfiles = Get-ChildItem -Path "$logpathfolder" -Include "*.$fileextension" -Recurse | Where-Object {($_.LastWriteTime -lt $limit) } 
  
foreach($log in $logfiles){
  $logname = $log.Name
  $logfilepath = $log.fullname
  Write-Host "Deleting Log file $logname, because it's older than $days" -ForegroundColor Gray
  Remove-Item -Path $logfilepath -Force -Confirm:$false 
}

Have a look on the complete script :
NOTE: please check your exchange install path and the path to the logging folder

#Set-Executionpolicy RemoteSigned
$logpathfolder = "C:\logging"
$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\"

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 -Include *.log,*.blg, *.etl -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
        $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"
    }
}
 
#logging section
$logpathfolder = "C:\logging"
[string]$date = Get-Date -Format "yyyy-MM-dd-HH-mm"
#protcol starting the script (never deleted)
"Script run on $date" | Out-File -FilePath "$logpathfolder\excleanup.txt" -Append
 
Start-Transcript -Path "$logpathfolder\$date-excleanup.log"
 
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)
 
Stop-Transcript
  
### removing old .log files 
$fileextension = "log"
Write-Host "removing logfiles older than $days days from logpath $logpathfolder "
$limit = (Get-Date).AddDays(-$days)
$logfiles = Get-ChildItem -Path "$logpathfolder" -Include "*.$fileextension" -Recurse | Where-Object {($_.LastWriteTime -lt $limit) } 
  
foreach($log in $logfiles){
  $logname = $log.Name
  $logfilepath = $log.fullname
  Write-Host "Deleting Log file $logname, because it's older than $days" -ForegroundColor Gray
  Remove-Item -Path $logfilepath -Force -Confirm:$false 
}

By the way you can use this cleaner for every log cleanup you might need it. Have fun cleaning up your old stuff automatically. If you like this post, please click on helpful.

Was this article helpful?
YesNo
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments