Skip to main content

One line Powershell command to delete files older than 30-days.

Get-ChildItem "C:\target\path\" -force -recurse | where-object {$_.LastWriteTime -le (Get-Date).AddDays(-30) -and !$_.PsIsContainer} | remove-item -force;

# Additionally, to delete any empty directories left behind after deleting the
# old files. <http://stackoverflow.com/questions/17829785/delete-files-older-than-15-days-using-powershell>
Get-ChildItem -path "C:\target\path\" -recurse -force | where-object { $_.PsIsContainer -and (Get-ChildItem -path $_.FullName -recurse -force | where-object { !$_.PsIsContainer }) -eq $null } | remove-item -force -recurse;