Delete files based on Age

The Get-ChildItem cmdlet is a powerful tool to search and find files on a Directory, such as old files (over certain age)

To delete all files on a directory based on age (remember to remove the -whatif safety), and filter based on extension:


# delete files older than date, recursively
# remove the whatif for execution

# define the age, in number of days.
$ts =5
# get file path to delete old files
$oldfilesPath = 'C:\Temp'
# for current directory you may use
# $oldfilesPath = '.'

# identify any type of filter. use *.* for all files
$extension = '*.bak'

Get-ChildItem -Path $oldfilesPath -Recurse -filter $extension | Where-Object {($_.LastWriteTime -lt ((Get-Date) - (New-TimeSpan -Days $ts)))} | remove-item -whatif

 

If you need to delete more than one extension (but not all of them), you would have to use this, less efficient command:


# delete files older than date, recursively
# remove the whatif for execution

# define the age, in number of days.
$ts =5
# get file path to delete old files
$oldfilesPath = "C:\temp"
# for current directory you may use
# $oldfilesPath = "."

$ManyExt = '.bak', '.trn'

Get-ChildItem -Path "$oldfilesPath" -Recurse | Where-Object {($_.Extension -in $ManyExt) -and ($_.LastWriteTime -lt ((Get-Date) - (New-TimeSpan -Days $ts)))} | remove-item -whatif

Notice that this time we filter based on extension, no wildcards needed

The above scripts will only delete files. After that operation is completed, you might need to delete empty folders

$LogPath = 'C:\temp' # replace this with your root path...
Get-ChildItem -LiteralPath $LogPath -Force -Recurse | Where-Object {
    $_.PSIsContainer -and `
    @(Get-ChildItem -LiteralPath $_.Fullname -Force -Recurse | Where { -not $_.PSIsContainer }).Count -eq 0 } |
    Remove-Item -Recurse -WhatIf

 

Leave a Reply

Your email address will not be published. Required fields are marked *