[CmdletBinding(SupportsShouldProcess=$True)] Param( [int]$CutoffDays = 150 ) $cutoffDate = (Get-Date).AddDays(-$CutoffDays) # get path to cached NuGet packages ("%USERPROFILE$\.nuget\packages") $nugetCachePath = Join-Path "$([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile))" ".nuget\packages" # get all package versions $packages = gci -Path $nugetCachePath | gci # get only those folders which weren't accessed since $cutoffDate, and also get their lengths $oldPackages = $packages | ? { $_.LastAccessTimeUtc -le $cutoffDate } | Sort-Object -Property LastAccessTime | select Fullname,LastAccessTime,@{Name="Length";Expression={ (gci $_.Fullname -Recurse -Force | Measure-Object -Property Length -Sum).Sum }} # delete those folder $oldPackages | select -ExpandProperty Fullname | Remove-Item -Recurse # output freed bytes "{0:n2} Mb freed" -f (($oldPackages | Measure-Object -Property Length -Sum).Sum / 1Mb)