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.

Conclusion

In this article, you learned how to cleanup Exchange logs automatically with a scheduled task. Make sure that the CleanupLogs.ps1 is placed 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 be deleted automatically. Did you enjoy this article? You may also like Connect to Exchange Online PowerShell with ISE using MFA. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect and IT Consultant. His specialism is designing and building complex enterprise environments. He started Information Technology at a very young age, and his goal is to teach and inspire others. Connect with ALI TAJRAN on social media. Read more »

This Post Has One Comment

Leave a Reply

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

Back To Top