PowerShell Script: Delete Files Older Than 7 Days


Another file operations PowerShell script that was recently required. This time we had a folder that contains a large number of log files that were never rotated and took up a large amount of disk space. These logs are only used for troubleshooting issues if they occur and it is rarely required for us to go over log files older than 7 days, so why are we keeping the rest?

Here is an easy one liner to help!

Until next time!

Get-ChildItem –Path “E:\TEMP\Logs” –Recurse | Where-Object{$_.LastWriteTime –lt (Get-Date).AddDays(-7)} | Remove-Item
Advertisement

2 thoughts on “PowerShell Script: Delete Files Older Than 7 Days

  1. Minh says:

    I tried to use your script and noticed that (getdate) should be (get-date). Thanks for the writeup. It made this super easy for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s