Removing Old Exchange 2013 Log Files

Posted on Posted in 2013, exchange, mcm, mcsm

Update: 18th Sept 2014. An updated version of this script has been written by Thomas Stensitzki and can be downloaded from http://www.sf-tools.net/Messaging/tabid/55/EntryId/213/Updated-script-to-purge-Exchange-and-IIS-log-files.aspx. This updated version works on systems that have not used the default installation paths and it reads them automatically from the server. The below still works for users with default installation paths.

Update 11th Dec 2014. Thanks to Antonio Vargas, this script now also removes the up to 5GB of performance log files that Exchange Server 2013 stores in “C:\Program Files\Microsoft\Exchange Server\V15\Logging\Diagnostics\DailyPerformanceLogs”. Again, if you have not installed Exchange to the C: drive use Thomas’s version above

Exchange 2013 creates log files for everything, and this is way more than in 2010. Everything is logged, and even when the server is doing nothing, it is recording the health of the server and writing that down to the logs.
The following PowerShell script removes log files (those named *.log) over 30 days old in the IIS logs folder and the same from the Exchange 2013 logging folder. Neither of these folders are cleaned up automatically in Exchange 2013 RTM or SP1. The transport logs in a different folder are cleaned up automatically after 30 days, so this script uses the same duration.

Set-Executionpolicy RemoteSigned

$days=30 #You can change the number of days here


$IISLogPath="C:\inetpub\logs\LogFiles\"

$ExchangeLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Logging\"


Write-Host "Removing IIS and Exchange logs; keeping last" $days "days"


Function CleanLogfiles($TargetFolder)

{

    if (Test-Path $TargetFolder) {

        $Now = Get-Date

        $LastWrite = $Now.AddDays(-$days)

        $Files = Get-ChildItem $TargetFolder -Include *.log,*.blg -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

        foreach ($File in $Files)

            {Write-Host "Deleting file $File" -ForegroundColor "Red"; Remove-Item $File -ErrorAction SilentlyContinue | out-null}

       }

Else {

    Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red"

    }

}


CleanLogfiles($IISLogPath)

CleanLogfiles($ExchangeLoggingPath)

The above script runs in PowerShell and will delete logs on the server that you are running the script on.

The following version of the same script will get all the Exchange 2013 servers in the organization and then delete the logs (older than 30 days) across all the servers for you from one machine. You run this second script from Exchange Management Shell (run as administrator) and need remote file access to C$ (or whichever folder you set in the script) to all the servers Exchange 2013 servers.

Set-Executionpolicy RemoteSigned

$days=30 #You can change the number of days here


$ExchangeInstallRoot = "C"

$IISLogPath="inetpub\logs\LogFiles\"

$ExchangeLoggingPath="Program Files\Microsoft\Exchange Server\V15\Logging\"


Write-Host "Removing IIS and Exchange logs; keeping last" $days "days"


Function CleanLogfiles($TargetFolder)

{

    $TargetServerFolder = "\\$E15Server\$ExchangeInstallRoot$\$TargetFolder"

    Write-Host $TargetServerFolder

    if (Test-Path $TargetServerFolder) {

        $Now = Get-Date

        $LastWrite = $Now.AddDays(-$days)

        $Files = Get-ChildItem $TargetServerFolder -Include *.log,*.blg -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

        foreach ($File in $Files)

            {

               # Write-Host "Deleting file $File" -ForegroundColor "Red"

                Remove-Item $File -ErrorAction SilentlyContinue | out-null}

        }

Else {

    Write-Host "The folder $TargetServerFolder doesn't exist! Check the folder path!" -ForegroundColor "red"

    }

}


$Ex2013 = Get-ExchangeServer | Where {$_.IsE15OrLater -eq $true}


foreach ($E15Server In $Ex2013) {

    CleanLogfiles($IISLogPath)

    CleanLogfiles($ExchangeLoggingPath)

    }

22 thoughts on “Removing Old Exchange 2013 Log Files

  1. Or you can use:
    $ExchangeLoggingPath=(Get-ItemProperty “HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\Setup\”).MsiInstallPath + “Logging\”

  2. Or use:
    $ExchangeLoggingPath=(Get-ItemProperty “HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\Setup\”).MsiInstallPath + “Logging\”

  3. Hi,
    thanks for the script, can you pleas send me the method to run this script, as i am new and unable to run it. also please send me the .ps1 of this script,
    thanks in advance.
    Regards
    movi

    movisajid@gmail.com

    1. PS1 scripts are just the PowerShell script as a text file with .ps1 as the extension (and not .txt). You run then in the PowerShell window (or in the case of the second of these scripts, from the Exchange Management Shell windows as it uses Exchange PowerShell extensions). To run in PowerShell change directory to the folder the script is in (“cd c:\scripts” for example) and then execute the script (“.\Remove-ExchangeLogFiles.ps1” for example). You do not need a PS1 download as you just copy the text on the screen into Notepad and save it as a PS1.

  4. Hi brian

    just want to know , if i will RUN this script. I hope it will not affect my running production environment.

    pl confirm because for my environment also C: drive getting full.

    1. It will not impact your environment. It just removes log files. If you want to test it in a lab enviromnent – please do, and if you backup the log folders for the first few times you run it, please feel free to do so.

      1. thanks for your input , I have tried this in my production environment without any problem. its works fine.

  5. Hi Brian

    Thanks for the scripts. I used the first script on my development instance and it cleared out 20 GB of log files and brought Exchange online again. I’ve now set this up to run as a daily task.

    Kind regards,

    Lee

    1. Becuase data is important. If you take your log files and store them in a big data processing ssytem then you can make observations from this data that you otherwise would not have. If you are not going to do this, then fine – delete the logs. But it would be remise of Microsoft to delete logs that might contain useful information to your enterprise.

      For example, imagine getting reports that the latest version of some mobile OS is causing a problem. Did it happen last week or last month with the previous version? How would you know if you did not have the ability to see the history of your service to your users.

  6. The method I use is a one line Scheduled Task using FORFILES CLI.

    1) Create a daily scheduled task with the following
    2) Action: Start a program
    3) Program: FORFILES
    4) Arguments: /p “C:\Program Files\Microsoft\Exchange Server\V15\Logging” /s /m *.* /c “cmd /c Del @path” /d -7

    Change the path above to where your E15 Logging folder is. Change the “-7” to control how many days to keep, “-7” = 7 days.

    Create a 2nd Task to delete IIS logs, by default in “C:\inetpub\logs\LogFiles”

Leave a Reply

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