PowerCleaning

I’ve often wanted to easily delete all old files below a folder that haven’t been modified for a while. I’ve written a very small PowerShell function to do just that. By default it emulates the Disk Cleanup tool in Windows and deletes all files older than one week from the environment temp folder.

function Remove-TemporaryItems ([string] $path = ($env:TEMP), [TimeSpan] $age = (New-Object System.TimeSpan 7, 0, 0, 0) ) {
$files = Get-ChildItem $env:TEMP -recurse | `
Where-Object { ! $_.PSIsContainer -and $_.LastWriteTime.Add($age) -le [DateTime]::Now };
$files | Remove-Item;
$files;
}