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”

Logs1

2.  Give it a name and description. Next

Logs2

3. Choose Daily. Next

Logs3

4.  Select a time for the task to run. Next

Logs4

5. Choose Start a Program. Next

Logs5

6. Browse to the Powershell.exe. Copy and paste the following to the “Add arguments (optional):” section.  Next

gci ‘c:\program files\microsoft\exchange server\V15\Logging’ -Directory | gci -Include ‘*.log’,’*.blg’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-7) | Remove-Item

Logs6

7. Tick the box to open the properties when the task is finished. Finish

Logs7

8.  Change the account to use SYSTEM. OK

Logs8

9. Repeat the process to create a second task using the parameters for the IIS Logs

gci ‘C:\inetpub\logs’ -Directory | gci -Include ‘*.log’,’*.blg’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-14) | Remove-Item

10. Once created, the tasks can be exported and imported onto other Exchange servers

Logs9

Advertisements

12 thoughts on “Using PowerShell & Task Scheduler to Purge Microsoft Exchange Logs

  1. Thanks for posting this script but it is not working for me not sure why. No files are deleted. Why does it have to call the RemoteExchange.ps1 script in order to do this?

    Like

    • Hi Guy, the script calls RemoteExchange.ps1 to make a remote powershell connection to Exchange. If you’re having troubling scheduling the script via task scheduler, start by opening powershell and running the script manually.

      Like

  2. Ensure the tick marks are single quotes and add the closing double quotes after Remove-Item:
    -NonInteractive -WindowStyle Hidden -command “. ‘c:\program files\microsoft\exchange server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; gci ‘c:\program files\microsoft\exchange server\V15\Logging’ -Directory | gci -Include ‘*.log’,’*.blg’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-7) | Remove-Item”

    You can see the difference by copying the above parameters into notepad and make the appropriate changes.

    Once I made these changes the script worked great!

    Like

    • Hi Caspar. Thanks for your feedback. I’ve had a look back over the powershell command, and in particular the parameters. After some testing, I noticed I had included some additional, unnecessary lines in the parameters i.e. – -NonInteractive -WindowStyle Hidden -command “. ‘c:\program files\microsoft\exchange server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; . The parameters should start with the Get-ChildItem cmdlet as follows gci ‘c:\program files\microsoft\exchange server\V15\Logging’ -Directory | gci -Include ‘*.log’,’*.blg’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-7) | Remove-Item.

      I have updated my blog with the corrected parameters. I have also tested on Exchange 2016 and verified it works. Please try again and let me know how you get on.

      regards
      Shane

      Like

  3. hello,
    thank you for this, however i cannot get this to work on server2016.
    it says the -NonInteractive command is invalid.

    Should anything be placed in front of this?

    Like

  4. Another Reply:

    The entire Syntaxc seems to be wrong, if i copy and paste it, and of coure- remove the remove-item at end of line, i’ll get much more files back then the command should.

    After changing it to:
    gci ‘c:\program files\microsoft\exchange server\V15\Logging’ -Include ‘*.log’,’*.blg’ -Recurse
    | ? LastWriteTime -lt (Get-Date).AddDays(-14)
    ill get the expected result.

    But there are more files, so i suggest you using following line:
    get-childitem ‘c:\program files\microsoft\exchange server\V15\Logging’ -include ‘*.log’,’*.blg’
    ,’*.bak’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-14) | Remove-Item

    Like

  5. I saw, “The item at Microsoft.PowerShell.Core\FileSystem::c:\Program Files\Micrsoft\Exchange Server\V15\Logging\CmdletInfra\Others has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?” To resolve this, I added the following after Recurse: -Attributes !Directory

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s