I don't know of any Explorer add-ons, but like most things in Windows, this can be done with PowerShell:
ForEach ($Drive in Get-PSDrive -PSProvider FileSystem) {
$Path = $Drive.Name + ':\$Recycle.Bin'
Get-ChildItem $Path -Force -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
Remove-Item -Recurse
}
Save this script as a text file with a .ps1
extension. You can then use Task Scheduler to run this at regular intervals.
First, though, you need to permit the execution of PowerShell scripts, because by default you can only execute commands typed directly into the PowerShell prompt. To do this, open PowerShell and type in the following command:
Set-ExecutionPolicy RemoteSigned
Type in "y" or "yes" when prompted. See Get-Help Set-ExecutionPolicy
for more information.
Now open Task Scheduler and create a new task with the following parameters:
- Under the "General" tab, enter a name and check the "Run with highest privileges" option
- Under the "Triggers" tab, add a new trigger and set the task to run daily
- Under the "Actions" tab, add a new action:
- leave the type as "Start a program"
- set the "Program/script" field to
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- set the "Add arguments" field to
-NonInteractive -File "C:\path\to\script.ps1"
- Under the "Conditions" tab, uncheck "Start the task only if the computer is on AC power"
Line-by-line explanation of the script:
ForEach ($Drive in Get-PSDrive -PSProvider FileSystem) {
This gets a list of all drives in the computer and loops through them one by one. The -PSProvider FileSystem
parameter is required to only return disk drives, because PowerShell also has pseudodrives for various other things like registry hives.
For more information, see Get-Help Get-PSDrive
and this tutorial on loop processing in PowerShell.
$Path = $Drive.Name + ':\$Recycle.Bin'
This constructs the path to the Recycle Bin folder on the current drive. Note the use of single quotes around the second part, to prevent PowerShell from interpreting $Recycle
as a variable.
Get-ChildItem $Path -Force -Recurse -ErrorAction SilentlyContinue |
This returns all files and subfolders under the given path (the one we constructed with the previous command). The -Force
parameter is needed to go into hidden and system folders, and the -Recurse
parameter makes the command recursive, ie. loop through all subdirectories as well. -ErrorAction
is a standard parameter for most PowerShell commands, and the value SilentlyContinue
makes the command ignore errors. The purpose of this is to prevent errors for drives that have been configured to delete files immediately. The |
symbol at the very end pipes the results to the next command; I split it up to several lines for better readability.
For more information, see Get-Help Get-ChildItem
.
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
This simply filters the results from the previous command and returns only those that are older than 30 days. $_
refers to the object currently being processed, and the LastWriteTime
property in this case refers to the date and time that the file was deleted. Get-Date
returns the current date.
For more information, see Get-Help Where-Object
and Get-Help Get-Date
.
Remove-Item -Recurse
This simply deletes the items passed to it by the previous command. The -Recurse
parameter automatically deletes the contents of non-empty subfolders; without it, you'd be prompted for such folders.
For more information, see Get-Help Remove-Item
.
It's not an explorer add on, and it's only 24 hours, but CCleaner has this option http://i.imgur.com/JaeJhzU.png
– Matthew Lock – 2014-11-18T04:16:03.390Perhaps, instead of setting a time limit, you could have the Recycle Bin automatically delete the oldest items when a certain size limit has been reached? Would this be acceptable? – William Jackson – 2012-06-09T16:44:54.033
3@WilliamJackson That can be a solution for those who have limited hard disk space. But in my case, I want to make sure that I won't regret emptying recycling file because of loss of a recently deleted file. With this system, I will just forget about the recycle bin, it will empty itself automatically, and I will have plenty of time to recover a deleted file if I happen to regret it afterwards. – hkBattousai – 2012-06-09T17:02:17.437
1On gnu/linux world, KDE has this feature, and I think it's great for usability. Why should I keep in the trash can files for more than, say, 1 month? This approach doesn't clutter your trash can with old files that won't be relevant to you any more, so it's a lot easier to find and recover one or more files when you need it (really useful if you work a lot wit text files, that are always small), and don't waste disk space with files no more useful to you. – gerlos – 2013-07-05T09:11:40.357