Log Cleanup with Powershell (May 7 2014)

It’s a very common task to need to remove all files in a path older then a number of days. Because of this there are countless methods to accomplish this task. Since I’m on a Powershell kick I decided to toss one together to handle a little log clean-up I needed to do. The goal is simple, given an array of paths delete any file that is older than the specified date and while we’re at it delete any empty folders.

A very basic check to see if $Delete is True/False specifies whether to actually perform the delete or not. Yes, there may be better or more elaborate methods but this gets the job done.


# *******************************************************************************
# File:                Log_Cleanup.ps1
# Purpose:        Recurse a directory and delete all files older then N days, as well as any empty folders
# Version:            1.0
# Date:                11:14 AM Friday, May 02, 2014
# Created by:    Jeff Frye
#*******************************************************************************
cls
$OlderThen = "120"                                                                                                                                                    # Specify the age in days
$arrPath = @("C:\logs\LogFiles",
"C:\ProgramData\Microsoft\Windows\SomeProgram\logs" )                                                                                            # Full root path,  Array
$Delete = $False                                                                                                                                                        # Set to $True to really delete files
	
#-----------------------------------------------------------------------------------------------------------------------------------------#
#----- No Config Below this Line -----#
#-----------------------------------------------------------------------------------------------------------------------------------------#
	
$limit = (Get-Date).AddDays(-$OlderThen )
	
foreach ($path in $arrPath)
{
	IF ($Delete -eq $True)                                                                                                                                            # Delete the Files
	{
		#----- Delete files older than the $limit. -----$
		Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
	
		#----- Delete any empty directories left behind after deleting the old files. -----$
		Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
	}
	Else                                                                                                                                                                        # Displays but does not Delete
	{
		#----- Delete files older than the $limit. -----$
		Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit }
	
		#----- Delete any empty directories left behind after deleting the old files. -----$
		Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null }
	
	}
}
Here be dragons, or a footer. Both work.