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 older than a certain number of days in any folder — here's how to do it on Windows 10.

PowerShell delete older files
PowerShell delete older files

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 helpful 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 changed in the last month. 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 command can cause to deletion of the wrong files.

Delete files older than X days on Windows 10 from PowerShell

To delete older files from a specific location on Windows 10, 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 command, change the "C:\path\to\folder" path with the folder location and change -30 for the age of the file since the last modification.

    PowerShell LastWriteTime command

Delete files older than X days automatically on Windows 10 from Task Scheduler

The previous command 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. You can always automate the process by creating a script and running it on a specific schedule using the Task Scheduler.

Create PowerShell script using Notepad

To create a PowerShell script to delete older than X days files on Windows 10, use these 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 command, change this “C:\path\to\folder” path with the folder location and “-30” for the age of the file since the last modification.

  4. Click the File menu.

  5. Choose the Save as option.

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

Create task using Task Scheduler

To run the PowerShell script automatically to delete old files with Task Scheduler, use these steps:

  1. Open Start.

  2. Search for Task Scheduler and click the result.

  3. (Optional) Right-click the “Task Scheduler Library” folder and select the New Folder option.

  4. Confirm a name for the folder and click the OK button.

  5. Right-click the folder and select the Create Task option.

  6. Confirm a name for the task in the “Name” setting.

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

    Task Scheduler General tab

  8. Clear the “Do not store password” option.

  9. Click the “Triggers” tab.

  10. Click the New button.

  11. Select the “On a schedule” option in the “Begin the task” setting.

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

  13. Click the OK button.

    Task Scheduler Trigger settings

  14. Click the Actions tab.

  15. Click the New Button.

  16. Select the “Start a program” option in the “Actions” setting.

  17. Type the following command in the “Program/script” setting:

  18. Type the following command in the “Add arguments” setting and click the OK button:

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

    Change the “C:\path\to\cleanup.ps1” path with the PowerShell script location you have previously created to delete files in the command.

    Task Scheduler Action settings

  19. Click the Settings tab.

  20. 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.
  21. Click the OK button.

  22. Confirm your administrative username and password (if applicable).

  23. 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.

About the author

Mauro Huculak is a Windows How-To Expert who started Pureinfotech in 2010 as an independent online publication. He has also been a Windows Central contributor for nearly a decade. Mauro has over 15 years of experience writing comprehensive guides and creating professional videos about Windows and software, including Android and Linux. Before becoming a technology writer, he was an IT administrator for seven years. In total, Mauro has over 21 years of combined experience in technology. Throughout his career, he achieved different professional certifications from Microsoft (MSCA), Cisco (CCNP), VMware (VCP), and CompTIA (A+ and Network+), and he has been recognized as a Microsoft MVP for many years. You can follow him on X (Twitter), YouTube, LinkedIn and Email him at [email protected].