PowerShell PackageManagement, how to uninstall a package provider?
Asked Answered
H

4

26

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?

Handclap answered 10/9, 2015 at 17:23 Comment(0)
J
25

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

Jesselyn answered 15/12, 2015 at 22:18 Comment(5)
Hoping for cmdlets for adding/removing providers in the future, but for now this seems to be the way.Handclap
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.Jesselyn
Some package providers can only be seen after importing like: Import-PackageProvider -name chocolateyget. Not sure why.Paperhanger
Is this still the way to go?Louise
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.Trigonous
K
3

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.

Koball answered 1/3, 2022 at 22:5 Comment(0)
F
1

A simple example of how to remove NuGet provider

(Get-PackageProvider|where-object{$_.name -eq "nuget"}).ProviderPath|Remove-Item -force
Restart-Computer
Fordo answered 12/8, 2020 at 16:12 Comment(1)
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.Koball
A
-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.

Alper answered 15/9, 2015 at 14:12 Comment(3)
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.Handclap
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) ...Megen
... Two links to Install-PackageProvider source: github.com/OneGet/oneget/blob/… and github.com/OneGet/oneget/blob/… further confirm suspicions... Not sure that there aren't ramifications like something left behind from bootstrapping that seems involved at least in some providers.Megen

© 2022 - 2024 — McMap. All rights reserved.