Junk files cleanup

How to delete files older than X days automatically using PowerShell

You can use PowerShell to free up space and keep things organized by only deleting files that are older than a certain number of days in any folder — here's how to do it on Windows 10.

PowerShell to delete old junk files automatically

On Windows 10, Storage sense is a feature to automatically free up space when you’re running low on storage. The feature works by deleting junk system files, those files that have been in the recycle bin or Downloads folder for more than a month, and making OneDrive content you have used in a while online-only.

Although this feature can be useful to control the storage space, it is limited and doesn’t offer an option to manually add different locations to monitor and delete files that haven’t change in the last month or so.

If you store non-important files in a different location, it is possible to use PowerShell and Task Scheduler to monitor and clean up files from any folder older than a specified number of days.

In this guide, you will learn the steps to automatically delete files that haven’t been modified in the last month or any number of days you specify on Windows 10. (These steps should also work on Windows 11.)

Important: It’s recommended to test the command using a temporary folder, as typing the wrong parameter can cause to deletion of the wrong files.

How to use PowerShell to delete files older than X days on Windows 10

If you have different folders with a lot of files and you would like to clean up by deleting those older than certain days, you can use these steps:

  1. Open Start on Windows 10.

  2. Search for Windows PowerShell, right-click the result, and select the Run as administrator option.

  3. Type the following command to delete files that haven’t been modified in the last 30 days and press Enter:

    Get-ChildItem –Path "C:\path\to\folder" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item

    In the above command, remember to change "C:\path\to\folder" specifying the path to the folder that you want to delete files and change -30 to select files with a last modified date.

    PowerShell LastWriteTime command

How to use Task Scheduler to delete files older than X days automatically on Windows 10

The command in the previous instructions allows you to delete files in a folder older than 30 days, but you need to open PowerShell and execute the command manually every time you want to free up space.

Creating a PowerShell script using Notepad

To run a task using the Task Scheduler, you’ll need to create a PowerShell script with the steps:

  1. Open Start.

  2. Search for Notepad and click the top result to open the experience.

  3. Copy and paste the following command into a Notepad text file:

    Get-ChildItem –Path "C:\path\to\folder" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item

    In the above command remember to change "C:\path\to\folder" specifying the path to the folder that you want to delete files and change -30 to select files with a last modified date.

  4. Click the File menu.

  5. Choose the Save as option.

  6. Save the file using the cleanup.ps1 name and extension.

Creating a task using Task Scheduler

If you want to automate the process, you need to use the Task Scheduler to create a task that executes the command at specified intervals.

  1. Open Start.

  2. Search for Task Scheduler and click the result.

  3. Right-click the Task Scheduler Library folder.

  4. Click the New Folder option.

  5. Type any name for the folder and click OK. (We’re creating a new folder to keep tasks organized and separated from the system tasks.)

  6. Right-click the recently created folder, and select the Create Task option.

  7. In the “Name” box, enter a name for the task.

  8. Under the “General” tab, under the “Security options” section, select the Run whether user is logged on or not option under the ” Security options ” section. (This option will make the command window not appear when the task runs automatically.)

    Task Scheduler General tab

  9. Clear the Do not store password option.

  10. Click the “Triggers” tab.

  11. Click the New button.

  12. Using the “Begin the task” drop-down menu, select On a schedule.

  13. Under “Settings,” specify when you want the task to run (for example, On time, Daily, Weekly, Monthly). Whatever option you select, make sure to specify the Start settings on the right side.

  14. Click the OK button.

    Task Scheduler Trigger settings

  15. Click the Actions tab.

  16. Click the New Button.

  17. Using the “Actions” drop-down menu, select the Start a program option.

  18. In the “Program/script” field, type the following command:

  19. In the “Add arguments” field, type the following command and click the OK button.

    -ExecutionPolicy Bypass C:\path\to\cleanup.ps1

    Remember to change "C:\path\to\cleanup.ps1" specifying the path to the PowerShell script you have previously created to delete files in the above command.

    Task Scheduler Action settings

  20. Click the Settings tab.

  21. Check the following options:

    • Allow task to be run on demand.
    • Run task as soon as possible after a scheduled start is missed.
    • If the task fails, restart everything.
  22. Click the OK button.

  23. Type your administrative username and password (if applicable).

  24. Click the OK button.

Once you complete the steps, the PowerShell script will run on the schedule deleting the files older than the number of days you specified. Remember not to change the name or move the folder to another location. Otherwise, the task will fail.