Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PowerShell cmdlets for publishing NuGet packages in a fast directory structure, and upgrading from an old flat directory listing of nupkg's to the faster one.
Function Create-NuPkgSha512File {
$sha512 = [Security.Cryptography.SHA512]::Create()
$fileBytes = [IO.File]::ReadAllBytes($Path)
$hashBytes = $sha512.ComputeHash($fileBytes)
$hashBase64 = [convert]::ToBase64String($hashBytes)
Set-Content -Path $Sha512Path -Value $hashBase64
Function Publish-Package {
$nupkgLeafName = Split-Path $Path -Leaf
$shell = New-Object -ComObject Shell.Application
$TempFolder = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath ([IO.Path]::GetRandomFileName())
$null = mkdir $TempFolder
$tempZipPath = "$TempFolder\$"
Copy-Item -Path $Path -Destination $tempZipPath
$zipItems = $shell.NameSpace($tempZipPath).Items()
$nuSpecItem = @($zipItems |? { $_.Path -like '*.nuspec' })[0]
$tempNuSpecPath = "$tempFolder\$([IO.Path]::GetFileName($nuspecItem.Path))"
$nuspec = [xml](Get-Content $tempNuSpecPath)
$id = $
$version = $nuspec.package.metadata.version
If (-not (Test-Path "$PackageSource\$id\$version")) { $null = mkdir "$PackageSource\$id\$version" }
Copy-Item -Path $tempZipPath -Destination "$PackageSource\$id\$version\$nupkgLeafName"
Create-NuPkgSha512File -Path $tempZipPath -Sha512Path "$PackageSource\$id\$version\$nupkgLeafName.sha512"
Copy-Item -Path $tempNuSpecPath -Destination "$PackageSource\$id\$version\$id.nuspec"
Remove-Item $TempFolder -Recurse -Force
Function Upgrade-PackageSource {
# Exclude symbols packages. NuGet clients don't use them from package sources anyway.
Get-ChildItem "$NuGet2Source\*.nupkg" -Exclude *.symbols.nupkg |% {
Publish-Package -Path $_ -PackageSource $NuGet3Source
Write-Host "Published $($_.Name)"
Copy link

AArnott commented Dec 3, 2015

This script has known bug NuGet/Home#1807.
The recommendation is to use nuget.exe v3.3, which includes new commands that replace the need for this PowerShell script:

PS Cmdlet NuGet command
Upgrade-PackageSource nuget init
Publish-Package nuget add

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment