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 Feb 03, 2023

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 Fix It Takes a Long Time to Delete Files in Windows

Why does Windows take so long to delete files and clear the recycle bin? This problem will occur when I am trying to delete plenty of small files on Windows. How can I fix this Windows issue?

Dany

Dec 27, 2022

How to Delete Files Older Than X days Automatically

Are you tired of useless files on your computer yet eating up space? Then use this simple approach to delete files older than x days using File Explorer, CMD, and PowerShell. We can also assist you in recovering accidentally lost files on Windows 11/10.

Dany

Jan 06, 2023

Use Shell Script to Delete the Files from Directory Based on Date Parameter

You can use a Shell script to delete the files from the directory based on the date parameter, but it is risky and easy to lose essential files. We will show you how to delete these files older than 2000 on your computer.

Dany

Jan 19, 2023

How to Delete Subfolders and Files by GPP

You can overcome this problem by configuring a subfolder item by GPP. Using the GPP options, you can configure a subfolder to a file system object. Before creating a subfolder preference item, you should investigate the behavior of each type of action supported by this extension.

Dany

Jan 06, 2023

How to Fix Files with Long Filenames Will Not Delete

When users change a short filename to a name that is too long, the file may not be deleted normally. The system will warn you that your filename is too long or your file cannot be found. We will help you out of this trouble in this passage.

Dany

Jan 18, 2023

The File Replication Service May Delete Files, What Can I Do

The DFS file replication may delete files; thus, you can refer to this post to learn how to avoid the circumstance by correctly using the command lines and Windows PC server. And after unintentional deletion, you can recover them with the help of the EaseUS Data Recovery Wizard.

Dany

Jan 12, 2023