46

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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
mit
  • 1,763
  • 4
  • 16
  • 27

4 Answers4

28

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.

E Bekker
  • 436
  • 6
  • 9
  • 2
    Royal pain in my backside but that appears to be the reason and solution. – Pecos Bill Feb 05 '18 at 02:45
  • 12
    If PowerShell is using the package and you need to exit PowerShell to remove it, how do you then run `Remove-Module` without restarting PowerShell and re-locking the package? – Keith Sep 20 '19 at 08:59
  • Using a different console, like windows cmd-prompt(for eg.) generally works when powershell is locking some module. – MrClan Mar 03 '20 at 12:41
  • 3
    In my case, an editor extension (PowerShell for VSCode) was the culprit preserving a module reference. Don't forget about those. – msanford Nov 01 '21 at 21:33
  • I had an AWS module installed in three different places for three different needs (user,admin,vscode) and had to uninstall them all. – danno Sep 02 '22 at 21:12
26

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.
14

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
Jon Davis
  • 6,562
  • 5
  • 43
  • 60
5

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
Andre Morata
  • 307
  • 3
  • 12