How To Use PowerShell to Automatically Delete Files Older Than a Certain Number of Days with Task Scheduler
Keeping your Windows system clean from unnecessary or outdated files can make a pretty noticeable difference — faster performance, more space, and just less clutter overall. While Storage Sense is handy, it doesn’t really let you pick individual folders like Downloads or Temp for cleaning, so if you want more control, setting up a task with PowerShell is a solid move. This method runs quietly in the background, so no manual cleanup needed. Just set it and forget it, for the most part.
Prerequisites
Before diving in, make sure you’ve got:
- Windows 10 or 11 (yeah, newer is better here).
- Admin rights to create scheduled tasks — because, Windows.
- A bit of comfort using PowerShell commands because we’ll need to tweak scripts.
Step 1: Open Windows Task Scheduler
You want to get into the Task Scheduler. Just type Task Scheduler into the search bar and hit Enter. If you’re feeling fancy, open it as an administrator — right-click and choose Run as administrator.
Step 2: Create a New Basic Task
In Task Scheduler’s dashboard, look for Create Basic Task on the right side and hit that. Name your task something descriptive like “Auto-delete old downloads.” Don’t forget to click Next!
Step 3: Choose When It Runs
Pick a schedule — I went with Daily because weekly felt too infrequent. Set the start time, make sure it’s a time when your PC is usually on, then hit Next. Good to go.
Step 4: Tell it what to do
This is where the magic happens. Select Start a program, then click Next. Type in PowerShell
for the program/script field.
In the Add arguments box, paste something like this (but make sure to replace the placeholders):
Get-ChildItem -Path 'C:\Users\YourName\Downloads' -File -Recurse -Force | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item -Force
Breakdown: change `’C:\Users\YourName\Downloads’` to whatever folder you care about, and swap `30` for whatever days you prefer. On some setups, this runs perfectly on the first try, but on others, you might see it fail silently or not delete stuff until you reboot or manually run it. It’s kinda weird, but that’s Windows for ya.
Step 5: Finalize
Hit Next again, review everything, and then click Finish. Easy enough.
Step 6: Test the Setup
Now, find the task you just made in the library. Right-click it and hit Run. A moment later, if everything’s set up right, your target folder should be cleaned up based on the rules you set. Watch out if files are still hanging around — sometimes, you need to tweak the script or permissions.
Extra Tips & Pitfalls
Because of course, Windows has to make it just a little harder than it should be:
- Double-check your
FOLDER_PATH
— if you mess this up, you might delete your entire Documents folder or something equally tragic. Better safe than sorry. Maybe test with a small dummy folder first. - If files don’t get deleted, verify that your user account has permissions to delete files in that directory.
- On some machines, the script works perfectly after a reboot. Others might require you to restart the Task Scheduler service or even the whole PC.
- If you want to delete files from multiple folders, create separate tasks for each, or write a more complex script to handle multiple paths.
Summary
- Use Task Scheduler to automate cleanup tasks with a tailored PowerShell script.
- Double-check folder paths and days to keep from deleting stuff you need.
- Test your setup by running the task manually first.
- Remember, permissions and system quirks can trip you up—be ready to troubleshoot.
Hopefully this shaves off a few hours for someone. Just something that worked on multiple machines. Fingers crossed this helps.