How to automatically delete files older than X days using PowerShell

In Windows 10, Storage Sense is a feature that automatically frees up storage space when storage space is running low. The feature works by deleting junk system files that have been in the Recycle Bin or Downloads folder for more than a month and only using OneDrive content that you’ve been using for a while online.

While this feature can be useful for controlling disk space, it is limited and does not offer the option to manually add different locations to monitor and delete files that have not changed in the last month or so.

If you save unimportant files elsewhere, PowerShell and the Task Scheduler let you monitor and clean up files from any folder that is older than a specified number of days.

This guide will walk you through the steps to automatically delete files that have not changed in the last month or a number of days you set on Windows 10. (These steps should work on Windows 11 as well.)

Important: It is recommended that you test the command with a temporary folder, as entering the wrong parameter can delete the wrong files.

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

If you have different folders with lots of files and you want to clean them up by deleting those older than certain days, you can follow these steps:

  1. Open minded begin under Windows 10.

  2. Search for Windows PowerShell, right click on the result and select the Execute as administrator Opportunity.

  3. Enter the following command to delete files that have not been modified in the last 30 days and press Enter:

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

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

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

You can use the command in the previous instructions to delete files in a folder that’s more than 30 days old, but you’ll need to open PowerShell and manually run the command each time you want to free up space.

Create a PowerShell script with Notepad

To perform a task using the Task Scheduler, you need to create a PowerShell script using the following steps:

  1. Open minded begin.

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

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

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

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

  4. Press the file Menu.

  5. Select the Save as Opportunity.

  6. Save the file with the cleanup.ps1 Name and extension.

Create a task with the task scheduler

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

  1. Open minded begin.

  2. Search for Task planner and click on the result.

  3. Right click on the Task Scheduler Library Folder.

  4. Press the New folder Opportunity.

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

  6. Right click on the recently created folder and select the Create task Opportunity.

  7. Enter a name for the task in the Name field.

  8. On the General tab, under the Security Options section, select Run whether the user is logged in or not Option in the “Security Options” section. (This option does not display the command window if the task is run automatically.)

  9. Delete the Do not save password Opportunity.

  10. Click the Triggers tab.

  11. Press the New Button.

  12. Select from the Start Task drop-down menu On a schedule.

  13. Under “Settings”, specify when the task should be carried out (e.g. on time, daily, weekly, monthly). Whichever option you choose, make sure you get the begin Settings on the right.

  14. Press the OK Button.

    Settings for the task scheduler trigger

  15. Press the Actions Tab.

  16. Press the New Button.

  17. From the Actions drop-down menu, choose the Start a program Opportunity.

  18. Enter the following command in the “Program / Script” field:

    powershell.exe
  19. In the Add Arguments box, enter the following command and click the button OK Button.

    -ExecutionPolicy Bypass C:pathtocleanup.ps1

    Remember to change "C:pathtocleanup.ps1" Provide the path to the PowerShell script you created earlier to delete files in the above command.

    Settings for task scheduler actions

  20. Press the settings Tab.

  21. Check the following options:

    • Allow the task to run if necessary.
    • Complete the task as soon as possible after missing a scheduled start.
    • If the task fails, restart everything.
  22. Press the OK Button.

  23. Enter your administrator username and password (if applicable).

  24. Press the OK Button.

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