How to clear Exchange Logs without any impact for Exchange 2016 (CU19) C Drive Disk space increase fastly.

Sathishkumar Singh 486 Reputation points
2021-04-09T11:12:43.76+00:00

Hello Support

I got the script for clearing logs. These are fine

is it safe to delete below the path of logs? after the backup completed?

Set execution policy if not set

$ExecutionPolicy = Get-ExecutionPolicy
if ($ExecutionPolicy -ne "RemoteSigned") {
Set-ExecutionPolicy RemoteSigned -Force
}

Cleanup logs older than the set of days in numbers

$days = 2

Path of the logs that you like to cleanup

$IISLogPath = "C:\inetpub\logs\LogFiles\"
$ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
$ETLLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2 = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\"
$ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\"

Clean the logs

Function CleanLogfiles($TargetFolder) {
Write-Host -Debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder

if (Test-Path $TargetFolder) {
    $Now = Get-Date
    $LastWrite = $Now.AddDays(-$days)
    $Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $_.Name -like "*.log" -or $_.Name -like "*.blg" -or $_.Name -like "*.etl" } | Where-Object { $_.lastWriteTime -le "$lastwrite" } | Select-Object FullName
    foreach ($File in $Files) {
        $FullFileName = $File.FullName  
        Write-Host "Deleting file $FullFileName" -ForegroundColor "yellow"; 
        Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null
    }
}
Else {
    Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red"
}

}
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)

But here i want mail notification like what are the files are deleted. those details to be sent by mail
Please advise how to add this Mail notification part? in the existing script

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,250 questions
{count} votes

4 answers

Sort by: Most helpful
  1. KyleXu-MSFT 26,211 Reputation points
    2021-04-12T03:38:16.627+00:00

    @Sathishkumar Singh

    Our forum does not support scripting on demand so far. If you need a script to complete a job, you may need to open a ticket to Microsoft which supported for it.

    About this existing script, you can modify those two places, then this script will generate and save a log:
    86677-qa-kyle-11-29-18.png
    86663-qa-kyle-11-29-48.png

    If you want to send it with email, you will need to writing another script to do it.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Sathishkumar Singh 486 Reputation points
    2021-04-12T12:00:58.963+00:00

    Thank you KyleXu

    is it safe to cleanup below paths of logs?

    $IISLogPath = "C:\inetpub\logs\LogFiles\"
    $ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
    $ETLLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
    $ETLLoggingPath2 = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\"
    $ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\"


  3. Andy David - MVP 142.7K Reputation points MVP
    2021-04-12T12:05:01.887+00:00

    yes, absolutely.
    The only logs you do not want to clear using a script are the Exchange database transaction logs.

    0 comments No comments

  4. Samdi 26 Reputation points
    2023-11-08T14:33:00.7933333+00:00

    If anyone else want to have a read about the script. The original article with the script can be found in the below post:

    https://www.alitajran.com/cleanup-logs-exchange-2013-2016-2019/

    0 comments No comments