How do I delete file types of a certain age?


This one liner will find all .txt files created more than 50 hours ago (make sure you change the path):


You could easily adjust the file type by replacing txt with whatever you like, as well as adjust the time range.  If you wanted to search based on the last modified date, you could replace $_.creationtime with $_.lastwritetime.


To have PowerShell delete files, you can just replace ft (Format-Table) with the Remove-Item cmdlet at the end:


Caution with this one – it has the power to cause havoc.


Leave a Reply

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