I am using the popular cleanuplogs script for my exchange server. All works fine. But I want to be able to set a different “olderThan” date for the certain folders.

is it best to make multiple tasks for each folder? OR is there a way i can set it ?

This is the script

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"

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)

3 Spice ups

The way that I would handle it would be to change your CleanLogfiles function to accept a second argument

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

    if (Test-Path $TargetFolder) {
        $Now = Get-Date
        $LastWrite = $Now.AddDays(-$TargetDays)
        $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 2
CleanLogfiles $ExchangeLoggingPath 3
CleanLogfiles $ETLLoggingPath 5
CleanLogfiles $ETLLoggingPath2 20

1 Spice up

Call the functions like this though

CleanLogfiles $IISLogPath 2
CleanLogfiles $ExchangeLoggingPath 3
CleanLogfiles $ETLLoggingPath 5
CleanLogfiles $ETLLoggingPath2 20
2 Spice ups

@haveyoutriedinverting

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

You just need to remove folder from this place:

Q_A_Kyle_16-07-32.pngDelete one folder at one time. Then set different time for those folders.

if the originally are like this :

CleanLogfiles($IISLogPath)

should i change it to :

CleanLogfiles($IISLogPath, 10)

or leave out the brackets and not use a , ?

You’re going to want to omit the parenthesis and the comma. Check out this answer on Stack Exchange for why.