Jun 122012

Delete Files older than a certain number of days with PowerShell

$date = get-date ; get-childitem | foreach {IF ($_.lastwritetime -lt $date.adddays(-30)) {Remove-Item $_.Name -WhatIf}}


this is perhaps an easier script, includes sub folders, only files with .log extension and older than 30 days. The -WhatIf describes what would happen if you executed the command without actually executing the command.


$date = get-date ; get-childitem -recurse -include *.log | Where-Object {$_.lastwritetime -lt $date.adddays(-30)} | Remove-Item -WhatIf


Leave a Reply