I’ve created a simple, re-usable function you can put in your PowerShell profile that will publish a package to NuGet or a local test feed.  This little script allowed me to remove redundant scripts that were scattered across my various projects.


I maintain a lot of packages on NuGet these days (seven at last count).  I hate having to remember exactly what steps to take when it’s time to push an update, so I’ve created PowerShell scripts to automate the process.  Unfortunately, each project has its own script.  If I make an improvement to one (such as adding support for pushing to my local NuGet test feed), I have to add it to all of them.  This finally became tedious enough that I ditched my scripts and added a function to my PowerShell profile.

Here’s the function:

function Publish-Package() {

    $ProjectName =  gci "*.csproj"

    $LocalTestFeedDir = "C:\Projects\LocalNuGetFeed\"

    #Remove existing packages
    Remove-Item *.nupkg
    #Create package
    nuget pack $ProjectName -Build -Properties Configuration=Release
    $PackageName = gci "*.nupkg"

    if ($PushToLocalFeed) {
        Write-Host -ForegroundColor Yellow "Deploying to local NuGet test feed!"
        Copy-Item $PackageName $LocalTestFeedDir
    elseif ($DoNotPush) {
        Write-Host -ForegroundColor Yellow "Skipping 'nuget push' step!"
    else {
        Write-Host "About to push to NuGet.org!  Press CTRL+C to abort."
        nuget push $PackageName
        Remove-Item *.nupkg

Execute the function in a directory containing your csproj file (and optionally your nuspec file), and it’ll package things up and push them to NuGet.org.  Or, you can specify the “PushToLocalFeed” flag to push to whatever local test folder you want.  And if you just want to create the package, but not do anything with it, you can specify the “DoNotPush” flag.

Oh, and if you aren’t keeping your PowerShell profile in the cloud yet, you really should be.  You can learn how to sync your profile using Dropbox in this post.