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?
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.
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.
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.
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.
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.
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.
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.
Update-InstalledModule
when needed?Import-Module
with the path of .psd1 file is likely to work. Still, it doesn't make Remove-Module
work 100%.Install-Module -Scope CurrentUser
, and/or restrict PowerShell usage with Set-ExecutionPolicy
, which can be run with -Scope
argument too.In my case, I solved this this way:
As of late 2021, I faced the same issue with pwsh7.2 and the PSFzf module. What worked for me:
pwsh -NoProfile
Uninstall-Module -Name PSfzf -Force
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With