Skip to content

Instantly share code, notes, and snippets.

@WimObiwan
Last active August 29, 2019 11:58
Show Gist options
  • Save WimObiwan/f8806893c0be17ebf5e3d9cab156e43a to your computer and use it in GitHub Desktop.
Save WimObiwan/f8806893c0be17ebf5e3d9cab156e43a to your computer and use it in GitHub Desktop.
CleanupOldFiles.ps1
$folder = 'C:\Temp\', $env:TEMP, '~/Downloads'
$threshold = (Get-Date).AddMonths(-1.0)
$ConfirmPreference = 'Low'
$oldFiles = $folder | Get-ChildItem -File -Recurse | ?{ $_.LastAccessTime -lt $threshold }
$oldFiles | Sort-Object -Property Length -Descending | Select -First 25 Length, FullName
$oldFiles | Measure-Object -Property Length -Sum -Maximum -Minimum -Average
$oldFiles | Remove-Item -Force -Confirm
$emptyFolders = $folder | Get-ChildItem -Directory -Recurse `
| ?{ $_.GetFiles().Count -eq 0 -and $_.GetDirectories().Count -eq 0 -and $_.CreationTime -lt $threshold }
$emptyFolders | Select FullName
$emptyFolders | Measure-Object
$emptyFolders | Remove-Item -Force -Recurse -Confirm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment