Skip to content

Instantly share code, notes, and snippets.

@RippieUK
Created January 15, 2020 11:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RippieUK/108a49239ae8572774af91b05cc8fafb to your computer and use it in GitHub Desktop.
Save RippieUK/108a49239ae8572774af91b05cc8fafb to your computer and use it in GitHub Desktop.
## Copied and cleaned up the script from https://gallery.technet.microsoft.com/Clear-Exchange-2013-Log-71abba44
## Tested on Exchange 2016
$executionPolicy = Get-ExecutionPolicy
if ($executionPolicy -ne 'RemoteSigned') {
Set-Executionpolicy RemoteSigned -Force
}
$days = 7
$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"
Function CleanLogfiles($TargetFolder)
{
Write-Host -ForegroundColor Yellow -BackgroundColor Black $TargetFolder
if (Test-Path $TargetFolder) {
$Now = Get-Date
$LastWrite = $Now.AddDays(-$days)
$Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $_.Extension -in '.log', '.blg', '.etl' -and $_.LastWriteTime -le $lastwrite } | Select-Object -ExpandProperty FullName
foreach ($File in $Files)
{
Write-Host "Deleting file $File" -ForegroundColor "yellow";
try {
Remove-Item $File -ErrorAction Stop
}
catch {
Write-Warning -Message $_.Exception.Message
}
}
}
else {
Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red"
}
}
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)
@uky2019
Copy link

uky2019 commented Mar 24, 2020

Hello, one question regarding variable $Targetfolder, what does it represent? function CleanLogfiles ($Targetfolder) but this variable is empty, where do we insert something in this variable?

*update: I see now it is #general variable for function which gets called later on the bottom of the scripts. Thanks any way :)

@thebtm
Copy link

thebtm commented Aug 31, 2020

Hey, just wondering if this script might be in a repo for improvements could be written and merged into a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment