There is a lot of focus on the Exchange Server Database, EDB, and transactional logs. But there are other logs we need to keep our eyes on. The services log was generated from Exchange Server 2016/2019 and IIS service logs. Cleanup these logs is also an important part of Exchange maintenance as these logs grow rapidly fast and consume a huge number of GB.

For this issue, I wrote a PowerShell script that will clean up IIS and Exchange Server Services Logs and ETL, which and over time consume much space.

Downloading the Script

You can download the script from GitHub, feel free to report issues and contribute there.

Click here to go to the download page

Update on 22-Feb-2022

  • Performance Monitor File .BLG files extension are also included in the files subject for removal. So the overall extensions are *.BLG, Log, ETL.
  • Minor fixes

Prerequisites

  • Windows PowerShell 5.1
  • The Script should run on Exchange Server
  • Exchange Snapin and WebAdministration. By default, these two modules are already available on Exchange Server.
  • The Script should run as Administrator.

Cleanup Exchange Logs script, Features, and Parameters.

The script can auto-discovers Exchange Services log paths and IIS Log paths. In other words, no need to change anything in the script. Also, the script has the following features and parameters:

  • JustCalculate: Get a summary of the total storage consumed by services logs (Exchange and IIS).
  • SimulateDeleteLogs: Simulate Deletion, which writes a log that lists all files subject to removal.
  • LogsOlderXDays: Only cleanup logs that are older than X number of days.
  • DeleteLogs: Delete the old logs and free disk space.
  • ExtraFolderToAdd: Include additional folders, such as Temp or any other folders for cleaning during the cleanup process (.Logs and .ETL files only).

There is no need to set Exchange Server in Maintenance mode, but if you need to, you can use my Exchange Maintenance Mode PowerShell module to handle all the steps safely and efficiently.

Examples

Using JustCalculate Parameter

Display a size summary of the total logs in each folder, this is the default parameter, so if PowerShell called the script without any parameter, it would trigger the JustCalculate parameter.

.\Clean-ExchangeLogFiles.ps1 -JustCalculate
Showing a quick summary of the total storage consumed by logs

Using SimulateDeleteLogs Parameter

The SimulateDeleteLogs writes to a log of all the files that are subject to removal. This will not cleanup any logs or perform any action against the logs.

If the –DeleteLogs parameter used with the –SimulateDeleteLog, the –DeleteLogs is ignored.

.\Clean-ExchangeLogFiles.ps1 -SimulateDeleteLogs
-SimulateDelete Parameter and the full logs

Using LogsOlderXDays Parameter

The LogsOlderXDays Parameter removes logs older than X number of days. For example, if logs older than 10 days should be removed, then use this parameter with an argument of 10, no need to add the minus sign.

The default value is 1 and can be set to a higher value

.\Clean-ExchangeLogFiles.ps1 -LogsOlderXDays 10

Using the ExtraFolderToAdd Parameter

Use the ExtraFolderToAdd parameter to include other folders to be cleaned, for example, the Windows Temp folder.

Keep in mind that ExtraFolderToAdd parameter is using the same filter, so it’s only for .LOG and .ETL files

The type of the ExtraFolderToAdd parameter is an array so the format of the argument is @(‘path1′,’path2’). Multiple paths can be added to the array using the comma as a separator. UNC path or Environment paths such as %TEMP% are not supported, the path should be the local path.

.\Clean-ExchangeLogFiles.ps1 -ExtraFolderToAdd @("C:\Windows\Temp","C:\root")
ExtraFolderToAdd Parameter

Using DeleteLogs Parameter

The DeleteLogs parameter triggers the Remove-Item operation and the script start removing logs and ETL files based on the folder provided.

Once the DeleteLogs start, NO user input confirmation is required. Instead, there is a Ten seconds countdown before the operation start. This makes it easier for using it in ScheduleTask so the script won’t require any user input

.\Clean-ExchangeLogFiles.ps1 -DeleteLogs
Removing Logs and ETL files

Its expected that the script may skip or fail in removing some files as they are in use.

File in use, it will be skipped.

Parameters Conjunction

The following parameter can be used together as they are in the same ParameterSet

  • DeleteLogs
  • SimulateDeleteLogs
  • LogsOlderXDays
  • ExtraFolderToAdd

And the following parameter can be used together

  • JustCalculate
  • LogsOlderXDays
  • ExtraFolderToAdd

Conculsion

I made this script to make the log removal very simple and won’t require changing any hard-coded value. Still, this is a script, and I might make some mistakes, use it at your own risk, and if you find any issue or bug, feel free and send a pull request or message me to fix it.

2/5 - (1 vote)