Most administrators come across cleanup tasks once in a while or when disk space is full. As we know most of the disk space gets occupied by log files and that could be any log files on windows or third party application running on windows.
In this article I will be showing very simple PowerShell cleanup script that will clean IIS 7 log files. One can use this sample and modify it to fit to other log files cleanup tasks.
I have separated cleanup functionality into a simple function called
CleanTempLogfiles() which is very generic. It takes simple file
path($FilePath) as parameter and removes all files older than specified days provided in the variable (
$days). In following sample script it removes all files older than 7 days.
# Module: Powershell script to clean IIS log files
Write-Host "Removing IIS-logs keeping last" $days "days"
Foreach ($File in Get-ChildItem -Path $FilePath)
if ($File.LastWriteTime -lt ($(Get-Date).Adddays($days)))
remove-item -path $File -force
Write-Host "Removed logfile: " $File