Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell 5.1 - How to uninstall module which is currently use

We are using some PowerShell modules in one deployment PowerShell script. Using following command we are installing module (i.e. XXXX) into "C:\Program Files\WindowsPowerShell\Modules".

Install-Module -Name "XXXX" -AllowClobber -RequiredVersion "XXXX" -Repository "XXXX" -Scope AllUsers

Now once we used the functionality of this module, we uninstall it at the end of deployment script using following command.

Remove-Module -Name "XXXX" -force
Uninstall-Module -Name "XXXX"  -AllVersions -force

But this uninstall module command gives following error.

WARNING: The version '###' of module 'XXXX' is currently in use. Retry the operation after closing the
applications.
PackageManagement\Uninstall-Package : Module 'XXXX' is in currently in use.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:2046 char:21
+ ...        $null = PackageManagement\Uninstall-Package @PSBoundParameters
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Power...ninstallPackage:UninstallPackage) [Uninstall-Packag
   e], Exception
    + FullyQualifiedErrorId : ModuleIsInUse,Uninstall-Package,Microsoft.PowerShell.PackageManagement.Cmdlets.Uninstall
   Package

Does anybody have any idea to resolve this?

like image 789
mit Avatar asked Mar 06 '17 13:03

mit


People also ask

How do I completely Uninstall a PowerShell module?

To uninstall the Az PowerShell module, you can use the Uninstall-Module cmdlet. However, Uninstall-Module only uninstalls the modules specified for the Name parameter. To remove the Az PowerShell module completely, you must uninstall each module individually.

Which removes a module that you added to the current session?

The Remove-Module cmdlet removes the members of a module, such as cmdlets and functions, from the current session. If the module includes an assembly ( . dll ), all members that are implemented by the assembly are removed, but the assembly is not unloaded.

Can't Uninstall PowerShell module?

To uninstall the PowerShell module, we can directly use the Uninstall-Module command but the module should not be in use, otherwise, it will throw an error. When we use the Uninstall-Module command, it can uninstall the module from the current user profile or from the all users profile.

Can I Uninstall a module?

The Uninstall-Module cmdlet uninstalls a specified module from the local computer. You can't uninstall a module if it has other modules as dependencies.


Video Answer


4 Answers

The problem could be that your existing PowerShell session is "locking" the module by loading possible elements from it (such as global variables or constants) even though you are trying to unload it (Remove-Module).

The cleanest way to be sure it isn't locked is to exit the PowerShell session. If you need to keep the session around to do "stuff" afterwards, trying starting a new PowerShell session (nested session) just before you make use of the module, then exit it at the end.

like image 113
E Bekker Avatar answered Oct 24 '22 06:10

E Bekker


As mentioned in the original answer by E Bekker, modules may get stuck in session. Closing the PowerShell session, and running uninstall in a new one should help.

Handling auto-loading

As Keith mentioned, a module may get locked if it's auto-loaded. If it happens via profile, adding -NoProfile to the uninstall script should help:

powershell -NoProfile -Command "Uninstall-Module X"

Some modules, like PSReadLine, get loaded even in such session, and may require extra -NonInteractive argument:

powershell -NoProfile -NonInteractive -Command "Uninstall-Module X"

I have yet to find such case, but if it still doesn't help, one may use (Get-InstalledModule -Name X).InstalledLocation to find where the module got installed, and remove it by other means (last resort). If another / older version of the module was bundled with PowerShell, it's best to avoid manual removal not to break it.

Workarounds

  • Maybe in this case uninstall is not necessary, and it's possible to install your module, keep it, and update it with Update-InstalledModule when needed?
  • Many modules can get imported without installation. Import-Module with the path of .psd1 file is likely to work. Still, it doesn't make Remove-Module work 100%.
  • If security is a concern, one can install the module only for the service user with Install-Module -Scope CurrentUser, and/or restrict PowerShell usage with Set-ExecutionPolicy, which can be run with -Scope argument too.
like image 36
Marcin Śmiałek Avatar answered Oct 24 '22 05:10

Marcin Śmiałek


In my case, I solved this this way:

  1. Close PowerShell.
  2. Really, close PowerShell. Open up Task Manager and find any background instances of PowerShell, as in either or both powershell.exe / pwsh.exe.
  3. Delete the unwanted module folders from C:\Users\${USER}\Documents\PowerShell\Modules
like image 9
Jon Davis Avatar answered Oct 24 '22 05:10

Jon Davis


As of late 2021, I faced the same issue with pwsh7.2 and the PSFzf module. What worked for me:

  1. Close/Force Close all instances of PowerShell/pwsh through task manager.
  2. Start a new instance of pwsh without profile. Win+R > pwsh -NoProfile
  3. Run Uninstall-Module -Name PSfzf -Force
like image 1
Andre Morata Avatar answered Oct 24 '22 07:10

Andre Morata