Clear logs in a single Exchange 2013 Server:



Set-Executionpolicy RemoteSigned

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

$IISLogPath="C:inetpublogsLogFiles"

$ExchangeLoggingPath="C:Program FilesMicrosoftExchange ServerV15Logging"

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 -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)

Clear Logs in multiple Exchange 2013 Servers at the same time:
  

Set-Executionpolicy RemoteSigned

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

$ExchangeInstallRoot = "C"

$IISLogPath="inetpublogsLogFiles"

$ExchangeLoggingPath="Program FilesMicrosoftExchange ServerV15Logging"

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 -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)

    }


Archives