Using PowerShell & Task Scheduler to Purge Microsoft Exchange Logs

Microsoft Exchange creates a lot of logs.  Unless these are managed, they can quickly fill up your disk space.  The following describes one way to use PowerShell and Task Scheduler to automatically purge the Exchange 2013 / 2016 and IIS logs:

Scheduled Task Summary

Task Name Purge Exchange logs older than 7 days
Function Deletes all Exchange logs older than 7 days from the following location ‘c:\program files\microsoft\exchange server\V15\Logging’
Schedule Daily at 1am
Program Called C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Parameters gci ‘c:\program files\microsoft\exchange server\V15\Logging’ -Directory | gci -Include ‘*.log’,’*.blg’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-7) | Remove-Item
Runs As SYSTEM
Task Name Purge IIS logs older than  14 days
Function Deletes all IIS logs older than 14 days from the following location ‘c:\inetpub\logs’
Schedule Daily at 1am
Program Called C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Parameters gci ‘C:\inetpub\logs’ -Directory | gci -Include ‘*.log’,’*.blg’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-14) | Remove-Item
Runs As SYSTEM

Step by Step

 1.  Open Task Scheduler and select “Create Basic Task” read more