denolfe
1/31/2018 - 6:00 PM

Powershell script to download all packages from a nuget feed

Powershell script to download all packages from a nuget feed

$destinationDirectory = "C:\LocalNuGetTest\"
$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object System.Net.NetworkCredential("USERNAME", "PASSWORD")
$feed =[xml]$webClient.DownloadString("https://hostednugetfeed.com/custom-feed/nuget/v2/Packages")

$records = $feed | select -ExpandProperty feed | select -ExpandProperty entry #| select -ExpandProperty content

for ($i=0; $i -lt $records.Length; $i++) {
    $content = $records[$i] | select -ExpandProperty content
    $properties = $records[$i] | select -ExpandProperty properties
    $title = $records[$i].title.'#text'
    $url = $content.src
    $startOfQuery = $url.IndexOf("?id=") + 4
    $fileName = $url.Substring($startOfQuery, $url.Length - $startOfQuery)
    $fullPath = ($destinationDirectory + "" + $fileName)

    $webClient.DownloadFile($content.src, ($destinationDirectory + "" + $title + "." + $properties.Version + ".nupkg"))
}