24

To troubleshoot a problem, I thought I'd try reinstalling the Chocolatey package provider. There appears to be no cmdlet to remove or uninstall a package provider. I'm not referring to removing a package source or package. I'm using PowerShell 5 on Windows 10.

Is there a way to uninstall a package provider?

briantist
  • 45,546
  • 6
  • 82
  • 127
Vimes
  • 10,577
  • 17
  • 66
  • 86

4 Answers4

23

Package providers are bundled with the WMF installation.

You can easily add package providers (and remove) if you know the search locations (even your own custom package providers).

Find where your package-provider is installed:

$p = (Get-packageProvider -name Chocolatey);
$p.ProviderPath

If you remove / move the assembly to somewhere outside the providers default search path; it will be unavailable (NB: Restart your host too see the effects).

Similarly can you add package providers by copying a exe / dll that implements the specifications for a packageprovider to the search location.

More documentation can be found here (implementing your own and the default search locations):

https://github.com/OneGet/oneget/wiki/Provider-assembly-search-locations https://github.com/OneGet/oneget/wiki/ImplementingPackageProvider

Bakudan
  • 19,134
  • 9
  • 53
  • 73
Harald F.
  • 4,505
  • 24
  • 29
  • 3
    Hoping for cmdlets for adding/removing providers in the future, but for now this seems to be the way. – Vimes Dec 16 '15 at 04:11
  • Agreed. I'm hoping this is something that's on it's way now that it's WMF5 is production ready. It would be useful to be able to add/remove package-providers with standard cmdlets, just as simply as adding / removing modules. – Harald F. Dec 16 '15 at 08:57
  • Some package providers can only be seen after importing like: `Import-PackageProvider -name chocolateyget`. Not sure why. – CMCDragonkai Dec 21 '16 at 00:21
  • Is this still the way to go? – mbx Aug 28 '17 at 09:23
  • fyi for anyone who runs into problems installing packageprovider nuget and gets errors about missing packagemanagement and related tags: if you are running the command with ps core (v6 or v7), then try running it with windows powershell instead first. – Subjective Reality Dec 14 '21 at 00:11
1

A simple example of how to remove NuGet provider

(Get-PackageProvider|where-object{$_.name -eq "nuget"}).ProviderPath|Remove-Item -force
Restart-Computer
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Spelling it out is helpful, but I don't think restarting the computer is required - starting a new PowerShell session should be enough. `Get-PackageProvider|where-object{$_.name -eq "nuget"}` can be simplified to `Get-PackageProvider nuget`. However, on Windows there's a catch-22: By running `Get-PackageProvider`, the DLL to delete gets loaded, which prevents its deletion. Also worth noting that _elevation_ (running as admin) is typically required for deletion to succeed. – mklement0 Mar 02 '22 at 15:13
1

To complement Harald F's helpful answer, given that the PackageManagement module as of version 1.4.7 still has no Uninstall-PackageProvider command (see all commands that come with the module with Get-Command -Module PackageManagement):

Note: To be able to undo this change later, make a note of the path reported by (Get-PackageProvider NuGet).ProviderPath and make a backup copy of that file.

Step-by-step instructions for removing the NuGet package provider, for example:

  • On Windows:

    • Copy the path of the NuGet package-provider assembly (DLL) to the clipboard:

      • (Get-PackageProvider NuGet).ProviderPath | Set-Clipboard
    • Start an elevated PowerShell session (run as admin - requires administrator credentials). To do that from an existing (non-elevated) session, run:

      • Start-Process -Verb RunAs (Get-Process -Id $PID).Path
    • Before continuing, close all other PowerShell sessions, which may include needing to exit Visual Studio Code.

      • Deleting the DLL will only succeed if no session has it currently loaded; if that isn't ensured, you'll get an Access denied error, even with elevation.
    • In the elevated session (in which you must not have submitted any PackageManagement commands), submit the following command to delete the NuGet package-provider assembly (DLL):

      • Remove-Item -Force <paste-the-previously-copied-path-here>
  • On macOS and Linux:

    • Start a PowerShell session with sudo. To do that from an existing (non-elevated) session, run:

      • sudo pwsh
    • Submit the following command to delete the NuGet package-provider assembly (DLL):

      • (Get-PackageProvider NuGet).ProviderPath | Remove-Item -Force
  • The remaining steps apply to all platforms:

    • Exit the elevated / sudo session.

    • Start a new (non-elevated) session for the change to take effect: Get-PackageProvider should then no longer list the NuGet provider.

mklement0
  • 382,024
  • 64
  • 607
  • 775
-1

If I understand what you want :

Uninstall-Package [-Id] [-RemoveDependencies] [-ProjectName ] [-Force] [-Version ] [-WhatIf]

Use the -Force option to forces a package to be uninstalled.

Bakudan
  • 19,134
  • 9
  • 53
  • 73
  • 5
    I think that is how to uninstall a package. But I want to remove a package-provider, for example the Chocolatey provider. It's the plugin that allows Package Management (aka OneGet) to interact with Chocolatey repositories. I'm becoming convinced that once a provider is added, there's no official way to remove it. At least at this point in time. – Vimes Sep 15 '15 at 15:55
  • From a quick skim of the code behind Install-PackageProvider this also seems to be installing Packages that have the PackageProvider inside (hence above suggesting removing assemblies, though I'd say nuke the whole folder which is what Unisntall-Package does).. and I tried it with the WinGet provider (Install-PackageProvider WinGet, Get-PackageProvider confirm its there, UninstallPackage WinGet, restart PowerShell 7.2 BTW, and Get-PackageProvider doesn't have WinGet anymore... also confirmed on disk the PackagePath as mentioned above the ProviderPath is removed (along w/ entire package) ... – Wes Oct 21 '21 at 03:27
  • ... Two links to Install-PackageProvider source: https://github.com/OneGet/oneget/blob/fb676d1390db52091784b89802107b7c5b258ab0/src/Microsoft.PowerShell.PackageManagement/Cmdlets/InstallPackageProvider.cs#L134-L136 and https://github.com/OneGet/oneget/blob/fb676d1390db52091784b89802107b7c5b258ab0/src/Microsoft.PowerShell.PackageManagement/Cmdlets/InstallPackageProvider.cs#L187-L189 further confirm suspicions... Not sure that there aren't ramifications like something left behind from bootstrapping that seems involved at least in some providers. – Wes Oct 21 '21 at 03:28