Skip to content

Instantly share code, notes, and snippets.

@djdefi
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djdefi/9677959 to your computer and use it in GitHub Desktop.
Save djdefi/9677959 to your computer and use it in GitHub Desktop.
Powershell script to clean out Exchange 2013 log files - Taken from http://www.c7solutions.com/2013/04/removing-old-exchange-2013-log-files-html
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 *.* -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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment