⚡ PowerShell 5.1+
Backup The NuGet packages cache to Zip
Zips up the current contents of the NuGet packages cache before you run a cleanup against it.
Backup-NugetCacheToZip.ps1
<#
.SYNOPSIS
Backs up the NuGet packages cache to a timestamped zip archive before you clear it.
.DESCRIPTION
Compresses the current contents of the NuGet packages cache into a zip file in
-DestinationPath, so you have a copy before running a cleanup script
against it.
.PARAMETER DestinationPath
Folder to save the zip archive into. Created if it doesn't exist.
.EXAMPLE
.\Backup-NugetCacheToZip.ps1 -DestinationPath D:\Backups
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$DestinationPath
)
$sourcePath = "$env:USERPROFILE\.nuget\packages"
if (-not (Test-Path $sourcePath)) {
Write-Host "$sourcePath does not exist on this machine." -ForegroundColor Yellow
return
}
if (-not (Test-Path $DestinationPath)) {
New-Item -ItemType Directory -Path $DestinationPath -Force | Out-Null
}
$timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$zipPath = Join-Path $DestinationPath "nugetcache_$timestamp.zip"
Compress-Archive -Path (Join-Path $sourcePath '*') -DestinationPath $zipPath -CompressionLevel Optimal -ErrorAction SilentlyContinue
if (Test-Path $zipPath) {
Write-Host "Backed up to $zipPath" -ForegroundColor Green
} else {
Write-Host 'Nothing was backed up (folder may have been empty).' -ForegroundColor Yellow
}