简体   繁体   中英

How can I run Nuget PowerShell cmdlet Install-Package in normal PowerShell session (not inside Visual Studio)?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM