4

I want to do some automation stuff and one of the steps is to install a Nuget package for a Visual Studio C# project. In Visual Studio, I can run the command Install-Package JetBrains.Annotations in the Package Manager Console panel. However, the same command doesn't work in a normal PowerShell session. How can I install a package for a Visual Studio C# project in a normal PowerShell session?

Just a learner
  • 26,690
  • 50
  • 155
  • 234
  • 1
    duplicate of this question: https://stackoverflow.com/questions/53912572/how-to-run-nuget-package-manager-host-in-cmd-powershell – zivkan Jun 28 '19 at 04:48
  • 1
    Maybe what you want is something like:[How How do I install the Nuget provider for PowerShell on a unconnected machine so I can install a nuget package from the PS command line?](https://stackoverflow.com/questions/35273109/how-to-use-a-nuget-package-within-a-powershell-script) and [Powershell Install Nuget package](https://stackoverflow.com/questions/36519123/powershell-install-nuget-package) . After you install and register the provider, a simple command like `install-package xxx -source https://www.nuget.org/api/v2` can install corresponding package for you. (Run as admin I suggest) – LoLance Jun 28 '19 at 06:32

1 Answers1

2

I would go with *-Package cmdlets. I've done some automation stuff with NuGet, where I had to choose between NuGet CLI or PowerShell *-Package cmdlets. Since nuget list --allversion doesn't work since Feb 2017 (see GitHub ticket). I decided to use the PowerShell cmdlets.

From PowerShell docu:

PS> Install-Package -Name NuGet.Core -Source MyNuGet -Credential Contoso\TestUser

You may have to define the provider name to NuGet via -ProviderName:

Specifies one or more package provider names to which to scope your package search. You can get package provider names by running the Get-PackageProvider cmdlet.

...

Accepted values: msi, Programs, msu, Bootstrap, PSModule, nuget, chocolatey

Reason why I wrote this answer though this question is marked as duplicate:

The stackoverflow answer marking this question as duplicate, refers to either use Visual Studios built in management console (which is implemented as seperate Powershell host, and therefore not useable via the standard PowerShell) or via the NuGet CLI tools (which didn't cover all my automation requirements). Using "*-Package` cmdlets are not covered by this answer.

UPDATE 1

Here is an example how to install log4Net NuGet package:

  Install-Package log4net -ProviderName NuGet -Source https://www.nuget.org/api/v2 -Scope CurrentUser -Force

The get more information what is installed in the background you can attach the -Verbose switch.

As far as I can remember you've to use the v2 API of NuGet.

Community
  • 1
  • 1
Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • Hi @Moerwald, I successfully installed NuGet.Core. But I have no idea how can I use it to install a package. Could you give an example please? – Just a learner Jun 28 '19 at 10:24
  • Thank you for the update. I'm trying it. Just to confirm, the command `Install-Package log4net...` installs log4net for the Visual Studio project in the current working directory and not just install it on the computer, right? – Just a learner Jun 28 '19 at 10:34
  • Per default it installs the package under `~\AppData\Local\PackageManagement\NuGet\Packages\log4net.2.0.8\log4net.2.0.8.nupkg`, but the cmldet has a `-Destination` parameter, try this one. – Moerwald Jun 28 '19 at 10:41
  • You can also try a mixup with Powershell and Cake (https://cakebuild.net/), where you do the visual studio stuff via cake, and call cakes build.ps1 from Powershell. – Moerwald Jun 28 '19 at 11:30
  • Assuming NuGet.Core is the package from nuget.org, it's version 2.14.0, which is from Visual Studio 2013, which is why it doesn't support v3 feeds and will be missing any feature added in NuGet 3.x and later, such as support from NuGet packages using SemanticVersioning's `+build` metadata, so you won't be able to install/upgrade the latest version of many packages this way. – zivkan Jun 28 '19 at 13:38