skip to Main Content

Cleanup Exchange logs automatically with scheduled task

How to combine PowerShell with a scheduled task to cleanup Exchange logs automatically? Why do we want to delete Exchange logs automatically? I always say that if you can automate the process, you should do it. In this article, you will learn how to clear Exchange logs automatically with a scheduled task.

Before creating the cleanup Exchange logs scheduled task

Download the CleanupLogs.ps1 script and place it in the C:\Scripts\ folder. Create a scripts folder if you don’t have one in the C:\ drive. Did you install Exchange in a different directory than the default path? Change the path in the script. Read more about the script in the article Cleanup logs Exchange 2013/2016/2019.

Automate the cleanup Exchange logs with task scheduler

After we confirmed that the CleanupLogs.ps1 script is in place, we can start automating the cleanup logs Exchange script.

Start Task Scheduler.

Cleanup Exchange logs automatically with scheduled task start task scheduler

Click the Task Scheduler Library and click New folder… Name the folder Exchange and click OK.

Cleanup Exchange logs automatically with scheduled task create folder

Click the created Exchange folder. Click Create Task…

We have three tabs that we are going to fill in. The tabs are General, Triggers, and Actions.

We start with the first tab General.

Give the task name CleanupLogs. Change user account to SYSTEM. Enable Run with highest privileges. Select Configure for Windows Server 2016 if you have a Windows Server 2016. If you have another Windows Server version running, select that.

Click the tab Triggers. Click New…

Select the settings Daily. Configure which time the script needs to start running. For example 22:00:00. Make sure that it’s Enabled and click OK.

Cleanup Exchange logs automatically with scheduled task new trigger

Click the tab Actions. Click New…

Make sure to copy and paste below to both of the fields.

Program/script: Powershell.exe

Add arguments (optional): -ExecutionPolicy Bypass C:\Scripts\CleanupLogs.ps1

Click OK.

Cleanup Exchange logs automatically with scheduled task new action

Click OK.

The clear Exchange logs task is scheduled. From now on, the task scheduler will automatically delete Exchange logs. In the next step, you will run the script to test if all is working great.

Run the cleanup Exchange logs task in task scheduler

Click the created task CleanupLogs and Click Run. The Last Run Result will show that The operation completed successfully. (0x0).

Cleanup Exchange logs automatically with scheduled task run task

Make sure to check if the CleanupLogs task did clear the logs in the following folders.

C:\Program Files\Microsoft\Exchange Server\V15\Logging\
C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\
C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\


In this article, you learned how to cleanup Exchange logs automatically with a scheduled task. Make sure that you place the CleanupLogs.ps1 PowerShell script in the C:\scripts\ folder. Create a new task in the task scheduler. Run the task and check that it did cleanup the Exchange logs. From now on, the Exchange logs will delete automatically.

Did you enjoy this article? You may also like Connect to Exchange Online PowerShell. Don’t forget to follow us and share this article.



ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 8 Comments

  1. I run the script either manually with Run with PowerShell and either with Schedule Task but I still found log files older than 2 days

    I even add to the 4 folders security to have Read + Write access to the SYSTEM users and also a separate user as you recommend in material where you only provide the script.

  2. Excellent, cheers. You’ve been my go-to guy for a while now. You’re the new Paul Cunningham from back when Practical365 was good.

  3. Hi Ali
    Well documented and would like to have more on windows server with regards to SCCM deployment.


Leave a Reply

Your email address will not be published. Required fields are marked *