Created
April 21, 2017 21:11
-
-
Save thoemmi/c76f1a5533fa86e631b2ed9bbc43c412 to your computer and use it in GitHub Desktop.
Script to delete old NuGet packages from %USERPROFILE%\.nuget\packages which haven't been accessed for 150 days
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
[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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment