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.)
- Delete files older than X days on Windows 10 from PowerShell
- Delete files older than X days automatically on Windows 10 using Task Scheduler
- Delete files older than X days automatically from Command Prompt
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:
-
Open Start on Windows 10.
-
Search for Windows PowerShell, right-click the result, and select the Run as administrator option.
-
Type the following command to delete files that havenāt 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, change the
"C:pathtofolder"
path with the folder location and change-30
for the age of the file since the last modification.
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:
-
Open Start.
-
Search for Notepad and click the top result to open the experience.
-
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, change this āC:pathtofolderā path with the folder location and ā-30ā for the age of the file since the last modification.
-
Click the File menu.
-
Choose the Save as option.
-
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:
-
Open Start.
-
Search forĀ Task Scheduler and click the result.
-
(Optional) Right-click the āTask Scheduler Libraryā folder and select theĀ New Folder option.
-
Confirm a name for the folder and click the OK button.
-
Right-click the folder and select the Create Task option.
-
Confirm a name for the task in the āNameā setting.
-
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.)
-
Clear the āDo not store passwordā option.
-
Click the āTriggersā tab.
-
Click the New button.
-
Select the āOn a scheduleā option in the āBegin the taskā setting.
-
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.
-
Click theĀ OK button.
-
Click the Actions tab.
-
Click the New Button.
-
Select the āStart a programā option in the āActionsā setting.
-
Type the following command in the āProgram/scriptā setting:
powershell.exe
-
Type the following command in the āAdd argumentsā setting and click the OK button:
-ExecutionPolicy Bypass C:pathtocleanup.ps1
Change the āC:pathtocleanup.ps1ā path with the PowerShell script location you have previously created to delete files in the command.
-
Click the Settings tab.
-
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.
-
Click theĀ OK button.
-
Confirm your administrative username and password (if applicable).
-
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.