Frequently Asked Question
If you have different folders with a lot of files and you would like to cleanup by deleting those older than certain days, you can use these steps:
- Open Start.
- 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:\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 that are 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:
- 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:\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. - Click the File menu.
- Select the Save as option.
- Save the file using the cleanup.ps1 name and extension.
Creating a task using Task Scheduler
If you want to automate the process
ADVERTISEMENT, you need to use the Task Scheduler to create a task that executes the command at specified intervals.
- Open Start.
- Search for Task Scheduler and click the result.
- Right-click the Task Scheduler Library folder.
- Click the New Folder option.
- 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.)
- Right-click the recently created folder, and select the Create Task option.
- In the “Name” box, enter a name for the task.
- In the “General” tab, under the “Security options” section, select the Run whether user is logged on or not option. (This is the option that will make the command window not to appear when the task runs automatically.)
Task Scheduler General tab - Clear the Do not store password option.
- Click the “Triggers” tab.
- Click the New button.
- Using the “Begin the task” drop-down menu, select On a schedule.
- 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.
- Click the OK button.
Task Scheduler Trigger settings - Click the Actions tab.
- Click the New Button.
- Using the “Actions” drop-down menu, select the Start a program option.
- In the “Program/script” field, type the following command:
powershell.exe
- In the “Add arguments” field type the following command and click the OK button.
-ExecutionPolicy Bypass C:\path\to\cleanup.ps1
In the above command remember to change
"C:\path\to\cleanup.ps1"
specifying the path to the PowerShell script you have previouly created to delete files.Task Scheduler Action settings - 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 missed.
- If the task fails, restart everything.
- Click the OK button.
- Type your administrative username and password (if applicable).
- Click the OK button.