Deleting files over a certain age

We sometimes find the need to delete all files from a folder over a certain age. For example we have a folder that our network scanner dumps its scans in. Ideally everyone would file their scans straight away, but it doesn’t always happen.

So we have the following script running every hour that removes all files from the folder that are over an hour old.

Get-ChildItem –Path "C:\scans" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddMinutes(-60))} | Remove-Item

You can change .AddMinutes to .AddHours, .AddDays or AddMonths as appropriate.

Leave a Reply

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