Home > Build, NuGet, PowerShell, Visual Studio > Automatically Create Your Project’s NuGet Package Every Time It Builds, Via NuGet

Automatically Create Your Project’s NuGet Package Every Time It Builds, Via NuGet

So you’ve got a super awesome library/assembly that you want to share with others, but you’re too lazy to actually use NuGet to package it up and upload it to the gallery; or maybe you don’t know how to create a NuGet package and don’t have the time or desire to learn.  Well, my friends, now this can all be handled for you automatically.

A couple weeks ago I posted about a new PowerShell script that I wrote and put up on CodePlex, called New-NuGetPackage PowerShell Script, to make creating new NuGet packages quick and easy.  Well, I’ve taken that script one step further and use it in a new NuGet package called Create New NuGet Package From Project After Each Build (real creative name, right) that you can add to your Visual Studio projects.  The NuGet package will, you guessed it, pack your project and its dependencies up into a NuGet package (i.e. .nupkg file) and place it in your project’s output directory beside the generated dll/exe file.  Now creating your own NuGet package is as easy as adding a NuGet package to your project, which if you’ve never done before is dirt simple.

I show how to add the NuGet package to your Visual Studio project in the New-NuGetPackage PowerShell Script documentation (hint: search for “New NuGet Package” (include quotes) to find it in the VS NuGet Package Manager search results), as well as how you can push your package to the NuGet Gallery in just a few clicks.

Here’s a couple screenshots from the documentation on installing the NuGet Package:

NavigateToManageNugetPackages   InstallNuGetPackageFromPackageManager

Here you can see the new PostBuildScripts folder it adds to your project, and that when you build your project, a new .nupkg file is created in the project’s Output directory alongside the dll/exe.

FilesAddedToProject     NuGetPackageInOutputDirectory

So now that packaging your project up in a NuGet package can be fully automated with about 30 seconds of effort, and you can push it to the NuGet Gallery in a few clicks, there is no reason for you to not share all of the awesome libraries you write.

Happy coding!

  1. John
    July 9th, 2013 at 05:54 | #1

    Hi, interesting project – i seem to be getting a error when i build like so:

    The command “REM Create a NuGet package for this project and place the .nupkg file in the project’s output directory.
    ECHO Building NuGet package in Post-Build event…
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command “& ‘C:\SVN\Chevron\Chevron RTRM\ISS.WebAPI.PubSub\ISS.WebAPI.PubSub\trunk\ISS.WebAPI.PubSub.Attributes\PostBuildScripts\New-NuGetPackage.ps1’ -ProjectFilePath ‘C:\SVN\Chevron\Chevron RTRM\ISS.WebAPI.PubSub\ISS.WebAPI.PubSub\trunk\ISS.WebAPI.PubSub.Attributes\ISS.WebAPI.PubSub.Attributes.csproj’ -PackOptions ‘-OutputDirectory “”C:\SVN\Chevron\Chevron RTRM\ISS.WebAPI.PubSub\ISS.WebAPI.PubSub\trunk\ISS.WebAPI.PubSub.Attributes\bin\Debug\”” -NonInteractive’ -DoNotUpdateNuSpecFile -NoPrompt”” exited with code 1.

    I have powershell 3.0 installed – maybe that is the problem? not an expert at ps by any stretch of the imagination…

  2. July 9th, 2013 at 09:41 | #2

    Hi John, PowerShell v3.0 is required, so that shouldn’t be the problem. This “exited with code 1” is a generic “running the batch script failed” error message. The bottom of this page (https://newnugetpackage.codeplex.com/wikipage?title=NuGet%20Package%20To%20Create%20A%20NuGet%20Package%20From%20Your%20Project%20After%20Every%20Build) shows how to find the real error message. Can you post that error message please. Also, it might make more sense for you to open an Issue on the CodePlex page (https://newnugetpackage.codeplex.com/workitem/list/basic) rather than continuing in these comments. Thanks.

  3. October 6th, 2013 at 04:58 | #3

    Thanks for wonderful work. I need your help very much.
    In your BuildNewPackage-RanAutomatically.ps1 file there is an option to provide additional parameter.

    I have provided -IncludeReferencedProjects as parameter and it works.

    Now i want to generate Symbol package so i need to pass -Symbols.

    My Question is how i can send two parameter or how i can get Symbol package also.

    Hope you will help me.

    Thanks in advance.

  4. October 8th, 2013 at 11:39 | #4

    @Hasibul Haque
    I have replied to the issue you opened up at https://newnugetpackage.codeplex.com/workitem/4. Let’s continue the discussion there.

  5. October 23rd, 2013 at 15:16 | #5

    That’a very cool module, save me a lot of time.
    Thanks a lot!

  6. Tom
    March 19th, 2014 at 18:02 | #6

    Hi I was just wondering how you handle transforms like $rootnamespace$ is there an automated way to generate the pp files with namespaces swapped out?

  7. March 20th, 2014 at 15:40 | #7

    Hey Tom, I actually have never looked at these transforms before, but found the docs at http://docs.nuget.org/docs/creating-packages/configuration-file-and-source-code-transformations.

    My extension simply calls the NuGet.exe with whatever additional parameters you specify in the BuildNewPackage-RanAutomatically.ps1 file. So transforms will work as they always have; I don’t provide any additional functionality related to them, yet ;).

  8. Tom
    March 20th, 2014 at 15:53 | #8


    Heya I managed to get it all sorted using a .targets file a .proj file and a batch to kick off a build it turned out to be a pretty clean solution.

  9. Grzegorz Mrozik
    April 7th, 2014 at 16:19 | #9

    Hi. Thank you for great tool.
    However, there is a problem with non-english Windows.
    Constants defined in line 290 of New-NuGetPackage.ps1 are valid only on english Windows. On localized Windows (polish version in my case) NuGet.exe output is also localized and pattern matching (e.g. line 1159) returns false.

  10. April 7th, 2014 at 16:48 | #10

    @Grzegorz Mrozik
    Yup, you are right. I’m not sure how to do localization in PowerShell, which is why I grouped all of those console messages together in the script; to make it easy for you to change them to their equivalent strings for your language. Basically you will need to perform the pack and push operations against NuGet.exe manually to see what strings the console spits out, and then change the script to use those strings.

  11. harre
    June 11th, 2015 at 01:36 | #11

    Awesome plugin. A little gotcha is that you need to change the packProperties to this
    $packProperties = “VisualStudioVersion=12.0”

    If you don’t do that msbuild won’t find msbuild targets for vs2013 project types, for example MVC project.

  12. Joel Damon Elkhorne
    June 15th, 2016 at 00:56 | #12

    Fantastic addition – makes developing much more productive 🙂

    If I want to publish to a private NuGet server for use in-house only via post-build event say for a new purpose-built build config [Pack], can I add a .nuspec in the project directory (to give me author and perhaps other bits and pieces and do:
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command “& ‘$(ProjectDir)_CreateNewNuGetPackage\DoNotModify\UploadNuGetPackage.ps1′”
    after the creation Powershell line?

    The reason I ask is that I don’t want a new package after every build but if I do want a package created I also want it published automatically so that any assemblies that rely on it will see updates in their NuGet management UI

  13. waqas
    October 18th, 2016 at 09:44 | #13

    How can we automate the transformations as well..?? what I mean is that I want to create the transformation files automatically when I build the project..I want to read from the local app.config or web.config and copy the settings into a new xdt file..

  14. Cli
    February 7th, 2017 at 14:48 | #14

    Thank you sharing this great tool with us, sir.

    I tried to upload created nuget package to a local gallery with $sourceToUploadTo = “local-gallery-address” in config.ps1. But it’s not working nor any errors reported. Am I misunderstanding something?

  15. February 17th, 2017 at 17:38 | #15

    You will need to manually run the “RunMeToUploadNugetPackage.cmd” file to actually upload the new version to your NuGet server. This is not ran as part of the build, as it would potentially result in sooooooo many packages being uploaded to the server, as well as increasing build times.

  1. November 9th, 2013 at 08:40 | #1

%d bloggers like this: