Last active
August 29, 2019 11:58
-
-
Save WimObiwan/f8806893c0be17ebf5e3d9cab156e43a to your computer and use it in GitHub Desktop.
CleanupOldFiles.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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