Copying a NuGet repository, all packages and all versions, using PowerShell

Today I needed to copy a full repo to my machine, and then I found some good ready-to-use commands in PowerShell. I created this based on the solution in this post, but it didn’t suffice for me as I would like to have all versions downloaded and not only the lastest.

It is simple, just we have to do it:

# Set the nuget source as trusted first, then it won't show any pop-ups
Set-PackageSource -Name NuGet-Source -Trusted

# Downloading...
Find-Package -AllVersions -Source NuGet-Source| ForEach-Object {
        Install-Package -Name $_.Name -MaximumVersion $_.Version -Destination 'C:\Temp\Nuget\' -Source NuGet-Source -SkipDependencies
}

 

Sometimes for some reason, the code above doesn’t return all packages from the remote repository. Then I found a way to do it from Package Manager Console within Visual Studio.
First I create a list of all packages this way (this code must be executed from Visual Studio, in Package Manager Console):

Get-Package -ListAvailable -AllVersions -IncludePrerelease -PageSize 122831 | ForEach-Object { $id = $_.Id; $_.Versions | ForEach-Object { ($id + ',' + $_.Version) } } > C:\Temp\Nuget\AllPackages.txt

Then I use PowerShell (out of Visual Studio):

Get-Content C:\Temp\Nuget\AllPackages.txt | ForEach-Object {
$package = $_.Split(',')
$id = $package[0]
$version = $package[1]

$path = ('C:\Temp\Nuget\' + $id + '.' + $version)

Write-Host $path
if(! (Test-Path -Path $path)) {
Install-Package -Name $id -MaximumVersion $version -Destination 'C:\Temp\Nuget\' -Source NuGetSource -SkipDependencies -AllowPrereleaseVersions
}
}