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 Apr 15, 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

What Are Prefetch Files, and Is It Safe to Delete Prefetch Files

Is it safe to delete prefetch files on a Windows PC? This article will tell you the answer. And if you want to recover the permanently deleted files on your Windows PC, keep reading.

Brithny

Apr 15, 2024

How to Recover Deleted AutoCAD Files

Recover deleted AutoCAD (.dwg) files if you have accidentally deleted or emptied them from the Recycle Bin. EaseUS data recovery software makes it easy to recover all files back for Windows users.

Dany

Mar 25, 2024

How to Delete Hacker Files to Free Up Space on Windows

Virus attacks and malware may cause chaos on your computer, and you may not be able to delete some files. We will help you remove the files with CMD and show you how to format the hard drive.

Dany

Mar 21, 2024

Is It Impossible to Completely Delete Files from the Computer

When you delete a file from your computer, it does not simply vanish - at least not immediately. The data may be recoverable. This guide will show you how to permanently delete files so they cannot be recovered in Windows 11/10/8/7.

Jaden

Mar 20, 2024

How to Recover Deleted Files Using Restore Previous Versions

If you accidentally or unintentionally deleted files and then emptied the Recycle Bin, don't worry! This article will introduce how to recover deleted files using Restore Previous Versions feature. Read on to learn detailed information.

Brithny

Mar 22, 2024

How to Recover Deleted File from WinSCP

You may find the ultimate solution here if you have lost or unintentionally deleted files from WinSCP. Read this post and use EaseUS Data Recovery Wizard to recover deleted file from WinSCP.

Finley

Mar 25, 2024