• ARTICLES
SEARCH

How-To Geek

How to Delete Files Older than X Days on Windows

image

We have already shown you how flexible the Linux shell can be, but that’s not to say Windows is any further behind. Here’s two techniques you can use depending on your shell preference, cmd or PowerShell.

PowerShell 3

Get-ChildItem –Path  “C:\Backups” –Recurse | Where-Object CreationTime –lt (Get-Date).AddDays(-5) | Remove-Item

PowerShell 2

Get-ChildItem –Path  “C:\Backups” –Recurse | Where-Object{$_.CreationTime –lt (Get-Date).AddDays(-5)} | Remove-Item

Explanation

  • Firstly we get FileInfo and DirectoryInfo objects in the Path C:\Backups.
  • FileInfo and DirectoryInfo objects both contain a CreationTime property, so we can filter the collection using that.
  • The –lt (less than) operator is then used to compare the CreationTime property of the objects with Get-Date (the current date) subtract 5 days.
  • This then leaves us with a collection of objects that were created more than 5 days ago, which we pass to Remove-Item.

Pro Tip

To see what will be removed you can use the –WhatIf parameter:

Get-ChildItem –Path  “C:\Backups” –Recurse | Where-Object CreationTime –lt (Get-Date).AddDays(-5) | Remove-Item –WhatIf

image

Command Prompt

While we recommend you use one of the PowerShell methods, without getting into any of the gritty details you can also do it from command prompt.

forfiles -p "C:\Backups" -s -m *.* -d -5 -c "cmd /c del @path"

Pro Tip

To see what files are going to be deleted you can use echo.

forfiles -p "C:\Backups" -s -m *.* -d -5 -c "cmd /c echo @file"

image

Taylor Gibb is a Microsoft MVP and all round geek, he loves everything from Windows 8 to Windows Server 2012 and even C# and PowerShell. You can also follow him on Google+

  • Published 12/24/12

Comments (8)

  1. NSDCars5

    Just removed all files made before yesterday in C:\Windows. DESTRUCTION!!

  2. miki

    Good to know, thanks

  3. Merlin

    @NSDCars5: No no, what you just did was clean up Windoze, not destruction… ;-)

    I only hope you didn’t really do that…

  4. Frank

    You may be interested in the last time the file was modified instead of when it was created. Replace CreationTime with LastWriteTime.

  5. Charlie

    great. can we automate this?

  6. David

    @Charlie Yes, put the command in a .ps or .bat file and then create a Windows Scheduled Task that will call the script on whatever schedule or trigger you desire.

  7. David

    For those with Vista+ or the Windows Resource Kit,

    Here is a method using RoboCopy:

    :: Create an empty temp directory and purge all files and folders in the current directory that are younger than 5 days old.
    :: See RoboCopy /? for all of the available options like /E for recursive. Remove /L to actually perform the deletions.

    RD /S /Q “%Temp%\Temp” 2>nul & MKDIR “%Temp%\Temp” && ROBOCOPY “%Temp%\Temp” “C:\Backup” /PURGE /MT /MAXAGE:5 /NS /NC /NJH /NJS /L

    Why, use robocopy? Because it has a ton of filter options :)

  8. Raine

    This doesn’t work if the path is a UNC path, even if you give the location a drive letter (map a drive to the location) it fails. Anyone know a way around this?

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!