How to use Powershell's Uninstall-Package to uninstall *a* specific package from *a* specific csproject - powershell

So I'm trying to automate some workflow and I'm running into issues with uninstalling/reinstalling a specific package on a specific csproject. I know this is possible with Package-Manager, but how would I achieve this within a powershell script? Here's what my script looks like so far:
# delete package folder from local feed
Remove-Item 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\<packageFolder>' -Recurse
# delete past package
Remove-Item '.\<package>.nupkg'
# pack new package
nuget pack '<nuspecPath>.nuspec' -NoDefaultExcludes
# push package to local feed
nuget add '.\<package>.nupkg' -Source 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages'
**# Everything below this is not currently working #**
# Remove project's outdated package
Uninstall-Package -Name '<packageName>' -Project '<pathToProject>' -Force
# Install new package to project
Install-Package -Name '<packageName>' -Project '<pathToProject>' -Source 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages' -Force
So, apparently -Project or -ProjectName are not recognized atrributes when using Uninstall-Package in powershell.. So, how do I specify powershell to uninstall a specific package on a specific csproject? Any help or insights would be much appreciated!
I tried the above code and kept getting complaints about -ProjectName attribute. I thought that this command would uninstall a nuget package from a specific project and then reinstall a new version of that package to the project.

The following answer is valid for .NET (Core) SDK-style projects, but does not work for "classic" .NET Framework projects (see this comment):
You may use the dotnet CLI to manage your projects and solutions.
Run the following command to list all packages:
dotnet list <PATH-TO-PROJECT> package
Run the following command to remove a package from the project:
dotnet remove <PATH-TO-PROJECT> package <PACKAGE-NAME>
See Install and manage NuGet packages with the dotnet CLI for more information.

Related

what is the correct way of installing Google.Apis.Calendar.v3 using PowerShell

Using PowerShell v7.3.1, I am trying to install the Google.Apis.Calendar.v3 library, but I get this error.
As suggested I run Get-PackageSource
I have also installed .NET Framework 4.8.1
I have also tried Install-Module PowerShellGet -Force, which successfully installed the module, but then when I try Install-Package Google.Apis.Calendar.v3 I get the same error.
what is the correct way of installing Google.Apis.Calendar.v3 using PowerShell?
Google.Apis.Calendar.v3 is a Google package. Which is hosted on NuGet
You don't appear to have NuGet set as a package source See: register-packagesource
register-PackageSource -Name MyNuGet -Location https://www.nuget.org/api/v2 -ProviderName NuGet

Download Nuget Package using Nuget.exe

Im working on an automation solution for transfering nuget packages from nuget.org to a private nuget feed. Is it possible to download nuget packages using nuget.exe? If i run the following command, it fails because it tries to install it to the directory, but i dont need to install the package i just need to download it.
.\nuget.exe install Microsoft.AspNetCore.App.Runtime.linux-x64 -source nuget.org -force
This throws the following error:
Error NU5000: Package 'Microsoft.AspNetCore.App.Runtime.linux-x64 7.0.1' has a package type 'DotnetPlatform' that is not supported by project 'C:\foo'.
I am looking for something like a -force option, so that i can just download the package without installing it.

Unable to install NuGet package provider in PowerShell Core on Linux

I'm trying to set up the NuGet package provider on Linux in PowerShell 7 so I can useInstall-Package to get a package from the NuGet Gallery. However, when I run:
Install-PackageProvider -Name NuGet -Force
I get the following error:
Install-PackageProvider: No match was found for the specified search criteria for the provider 'NuGet'.
The package provider requires 'PackageManagement' and 'Provider' tags. Please check if the specified
package has the tags.
I did a bit of searching and found a few questions from this site and others where this error occurs, some answers saying I need to force TLS 1.2:
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
some saying to specify -RequiredVersion on Install-PackageSource, some saying to use-ForceBootstrap, and some saying to use -Force. None of these work and I'm still met with the same error each time. Get-PackageProvider lists NuGet as a provider.
I'm was also unable to install the NuGet provider on Windows using PowerShell Core with the same error. Is this just not supported from PowerShell Core?
I also received this error and specifying the version (currently 3.0.0.1) also fails. What worked for me was piping the packageprovider into Install-PackageProvider
Get-PackageProvider | where name -eq 'nuget' | Install-PackageProvider
You may add -Force if you want to avoid answering yes to The package(s) come(s) from a package source that is not marked as trusted. Are you sure you want to install software from ''?
The NuGet location has been updated. You can also do it in one line:
Register-PackageSource -Name nuget.org -ProviderName NuGet -Location "https://api.nuget.org/v3/index.json" -Trusted
Having TLS12 set in conjunction with Install Nuget package worked for me.
Below is how I used the command:
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet
In addition to #DougMaurer's answer, I also had to configure the package source as well:
$sourceArgs = #{
Name = 'nuget.org'
Location = 'https://api.nuget.org/v3/index.json'
ProviderName = 'NuGet'
}
Register-PackageSource #sourceArgs

install-package with chocolatey never adds anything to the path environment variable

The command install-package <packagename> -providerName chocolatey never seems to add anything to the PATH envirnoment variable (Windows 10 Build 15063).
For example running
install-package docfx -providerName chocolatey
install-package wkthmltopdf -providerName chocolatey
does not do it. Where as
choco install docfx
choco install wkhtmltopdf
does it. When I run the latter the commands docfx and wkhtmltopdf are available in the command line.
So my question is: How is installation different when using the choco command directly compared to install-package and what can I do about this?
As Rob Reynolds pointed out to me on gitter that package provider is not ready for prime time:
do not use the
prototype provider for OneGet/PackageManagement - it's not official,
it's a non-fully featured prototype made by MS and it may have
security issues. See
https://github.com/chocolatey/chocolatey-oneget/issues/5#issuecomment-275404099
for details

Let Teamcity serve multiple versions of the same nuget package

I try to have the Teamcity Nuget server provide version 1.0.0.1 and 1.0.0.2 of a package.
I built the same build configuration with different version numbers in NuGet Pack -> Version but only the version of the latest build is listed when I check with Get-Package -ListAvailable -Filter mycompany in package manager console.
I also tried with one build configuration for version 1.0.0.1 and another for 1.0.0.2, but still only the package of the latest build is listed.
How can teamcity serve multiple versions?
Try adding the -AllVersions parameter. The following seems to work with Team City but not for the main nuget.org.
Get-Package -ListAvailable -Filter mycompany -AllVersions