1 minute read

Note: This script is better than just ls -rec | measure, because measure measures only one field, and when iterating over many files and directories, every iteration counts.

Whenever our roaming profiles folder gets too big, we have to prune the profiles that are both old and heavy. Using windows explorer, it’s easy enough to see the size of a specific folder, but the last modified date is misleading, because files in that folder may have changed while the folder stayed the same. PowerShell to the rescue!

Searches and possibly deletes old and big folders
This script recursively checks folders for files, marking for every first-level folder
the sum of all of it's files size, and the latest WriteTime of a file.
If required, the script deletes folders that are both old enough and big enough.
The location of the folder to scan. Default is current folder.
Whether to delete the matching folders, or just to point them out
The minimum size of a folder to be deleted, in MBs. Default is 5MB
The minimum age of a folder to be deleted, in years. Default is 1 year
My Blog:
pushd $Path
$res = $null
ls . | %{
$totalSize = 0;
$lastModified = $_.LastWriteTime;
ls $_ -Recurse | %{$totalSize+=$_.Length;if($lastModified -lt $_.LastWriteTime){$lastModified = $_.LastWriteTime} }      
$_ | Add-Member -MemberType noteproperty 'TotalSizeMB' (($totalSize/1024/1024).toString('F'));
$_ | Add-Member -MemberType noteproperty 'LastModified' $lastModified;
$del = ((([int]$_.TotalSizeMB) -ge $MinSizeMB) -and ($_.LastModified.addYears($MinYearsOld) -le [datetime]::now))
$_ | Add-Member -MemberType noteproperty 'ShouldDelete' $del;
if($Delete -and $del){rm $_ -Recurse -Force;$_;}
} | tee -var 'res' | select name,TotalSizeMB,LastModified,ShouldDelete | out-gridview