How to Delete Files Older Than 30 Days with PowerShell

I need a script that will remove any files older than 30 days from a folder while leaving any files and directories with files within that are younger than 30 days. Any ideas for deleting files older than 30 days with PowerShell or other tools?

Best Answered by

Dany· Answered on Jan 04, 2024

To delete files older than 30 days ( we will take Windows 10 as an example), you can use PowerShell, Command Prompt, and File Explorer:

1. Right-click your search result for Windows PowerShell and choose "Run as administrator" after opening the Start menu on Windows 10. Type in Get-ChildItem –Path "C:\\path\\to\\folder" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item in the window. Replace C:pathtofolder with the folder location containing the files you wish to remove, and change "-30" to the command above that selects files with the latest updated date. Then the command will run after pressing the Enter key.

2. Run Command Prompt as an administrator on your Windows 10. Type in ForFiles //p "C:pathtofolder"//s //d -X //c "cmd //c del //q @file and hit Enter. Change C:pathtofolder to the path to the folder you want to remove files from, and change "//d -X" to the option you wish to use to only choose files with the last updated date. This command will help you delete files older than x days.

3. Delete files older than 30 days with Task Scheduler is the simplest way for novice users. To launch File Explorer, press Win + E on your keyboard and find the target folder. Click "Date Modified", choose the files, and delete them.

If you want to recover deleted files, you can download a third-party recovery tool. It is possible to recover deleted files from Recycle Bin, recent backups, or restore your system to a previous date. With three simple steps, you can try EaseUS Data Recovery Wizard to get back your lost films, Word documents, music, and personal pictures.

Use EaseUS Data Recovery Wizard to recover deleted or lost files easily and quickly.

Tutorials on how to recover lost, deleted, or formatted data from HDD, SSD, USB, SD card, or other storage media on Windows and Mac.

People Also Ask

How to Delete Photoshop Temp Files on Windows 10

If your hard drive gets full because of Photoshop temp files, you must learn about this page. You can know the Photoshop temp files' location and whether it is safe to delete Photoshop temp files on Windows 10. Then, you can delete Photoshop temp files with ease.

Dany

Jan 04, 2024

How to Recover Latest Windows Update Deleted Files

After the latest Windows update, it is possible that some files are deleted. We will introduce some available methods in this passage to help you recover the latest Windows update deleted files.

Dany

Jan 04, 2024

How to Recover Deleted Files from Sony A7III SD Card on Mac

It is easy to delete photos or videos accidentally on Sony A7III cameras. The files will not be erased immediately from your Sony A7III SD card until writing new data on this SD card. Let us find a way to recover files from a Sony A7III SD card.

Dany

Jan 04, 2024

How to Recover Bitdefender Deleted Files Without Asking

Bitdefender deleted files without asking? Want to restore files deleted by Bitdefender? Read this post to learn why this issue happens, how to use the EaseUS Data Recovery Wizard to recover deleted files, and how to stop important files from being deleted by Bitdefender.

Jaden

Jan 04, 2024

How to Recover Crashed Excel Files or Restore Its Previous Version

This post includes complete solutions to help you perform Microsoft Excel file recovery. If you accidentally deleted Excel files, lost an unsaved Excel file, or Microsoft Excel 2016 crashed on saving or opening files, relax. Follow the fixes here to restore your lost Excel files with ease now.

Jaden

Jan 04, 2024

Do Delete Files from SD Card Go to Recycle Bin on Windows

Do delete files from SD card go to Recycle Bin? After deleting some files from your SD card, you can't find the deleted files in Recycle Bin. If you want to know where the deleted files from the SD card go, this article will tell you the answer.

Brithny

Jan 04, 2024