Updated March 4, 2023
Introduction to PowerShell uninstall module
Uninstall-Module cmdlet in PowerShell which ships with PowerShellGet module, uninstalls the specific module (multiple modules as well) and specific version that is installed on the local computer in the local user profile or the current user profile and it fails to uninstall when the module has the dependency on the other modules and with the PowerShell remoting methods, we can uninstall module on the remote computers as well.
Syntax
Uninstall-Module Syntax:
Uninstall-Module
[-Name] <String[]>
[-MinimumVersion <String>]
[-RequiredVersion <String>]
[-MaximumVersion <String>]
[-AllVersions]
[-Force]
[-AllowPrerelease]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Uninstall-Module
[-InputObject] <PSObject[]>
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Syntax explanation:
-Name: Modules to uninstall. You can provide multiple module names, separated by comma (,).
-MinimumVersion: When there are multiple versions of the module installed, this parameter uninstalls the minimum version of the module. The MinimumVerion and RequiredVersion can’t be used together in the same command.
–MaximumVersion: This module helps to uninstall the latest or the maximum version of the module. The MaximumVersion and the RequiredVersion can’t be used in the same command.
-RequiredVersion: Specifies the exact module versions to uninstall.
-AllVersions: This parameter uninstalls all the available versions of the module installed on the local computer. This parameter can’t be used with MaximumVerion, MinimumVersion, and RequiredVersion parameter.
-AllowPrerelease: It uninstalls the module that is marked as PreRelease.
-Force: Forces to uninstall without asking for user confirmation.
-Confirm: If specified $true then it confirms for user consent and $false will not prompt the user for uninstallation and start the uninstall process.
How to use Uninstall module in PowerShell?
When the PowerShell module is installed on a computer, there is an option for the module to available for the current user or all the users. PowerShell chooses the different windows profile for them.
For the current user: “%UserProfile%\Documents\WindowsPowerShell\Modules”
For all the users: C:\program files\WindowsPowerShell\Modules
And there is another path which windows OS uses by default to store the PowerShell module when OS gets built.
OS default path for PowerShell: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
When we use the Uninstall-Module command, it selects the above paths to uninstall the modules from the local computer. There are some cases when multiple versions for the same module are installed because some scripters prefer to update when the new module arrives. In that case, Uninstall-Module helps to uninstall the specific version of the module using -RequiredVersion, -MinimumVersion, or -MaximumVersion command.
Sometimes the beta version of the module is also available for few users that is prerelease version and you can uninstall that specific module as well with -PreRelease parameter.
When the modules are in use or it is loaded and when you uninstall it, it will deny access, and to remediate this you need to close the open PowerShell sessions, or sometimes the system reboot may require.
Examples
Here are the follwoing examples mention below
Example #1
Uninstall ImportExcel Module.
For this example, we have the ImportExcel module installed in the local computer and we will uninstall it using the Uninstall-Module command.
Uninstall-Module ImportExcel -Verbose
Output:
Please note: We haven’t provided here the module version to uninstall and it doesn’t matter here if the module is for all users or the current user profile. It simply uninstalls the module.
Another way to uninstall the module is to get the module first using the Get-Module command and passing the output to the Uninstall-Module using Pipeline.
Get-Module -Name ImportExcel | Uninstall-Module -Verbose
The above command is similar to the first command.
Sometimes the module is installed in your system but not loaded in the PowerShell session then you can’t uninstall the module. For example, the ImportExcel module is installed but not loaded in PS console as shown below.
Get-Module -Name ImportExcel
The output is nothing but when we check, all the available modules, it shows the output.
Get-Module Importexcel -ListAvailable
Output:
In this case, you can either uninstall the module by first importing the module and then running uninstall command.
Get-Module -Name ImportExcel -ListAvailable | Import-Module -Verbose
Uninstall-Module ImportExcel -Force -Verbose
Or the easy method is to use the Get-InstalledModule command and Uninstall it, as shown below.
Get-InstalledModule -Name ImportExcel | Uninstall-Module -Force -Verbose
Example #2
Uninstall the Specific module version.
If we have multiple versions of the same module installed in the local system then we can use the -RequiredVersion parameter to uninstall the specific module version.
In this example, we have a 7Zip PowerShell module called “7Zip4PowerShell” installed and it has multiple versions in the same system.
Get-Module 7zip4PowerShell -ListAvailable
Output:
We need to uninstall the 1.5.0 module here.
Get-InstalledModule 7zip4PowerShell -RequiredVersion 1.5.0 | Uninstall-Module -Force -Verbose
Output:
When you check available versions for this module, you won’t find this version.
Example #3
Uninstall all the available versions of the module.
To uninstall all the available versions of the module, we can use the -AllVersions parameter. We have a 7Zip4PowerShell module with multiple versions installed in the local computer as shown below and we need to uninstall all of them.
To uninstall all module versions,
Get-InstalledModule 7zip4PowerShell -AllVersions | Uninstall-Module -Force -Verbose
Output:
Example #4
Uninstall module with Minimum and Maximum version parameters.
When we use the -MinimumVersion parameter, the Uninstall-Module removes the highest installed version from the computer. We have the below versions installed of the 7Zip4PowerShell module.
Get-InstalledModule 7zip4PowerShell -MinimumVersion 1.5.0 | Uninstall-Module -Force -Verbose
Output:
When we use the -Maximumversion parameter, it uninstalls the Lowest version of the module.
Get-InstalledModule 7zip4PowerShell -MaximumVersion 1.5.0 | Uninstall-Module -Force -Verbose
Output:
Example #5
Uninstall module on a remote computer.
We can use the Invoke-Command module to uninstall the module from the remote server.
Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock { Get-InstalledModule 7zip4PowerShell | Uninstall-Module -Force -Verbose}
You can use the other commands as shown in the above examples to work with the specific module version uninstall task.
Conclusion
Uninstall-Module is useful for clean-up tasks. There are many modules available in the PowerShell Gallery and you can download them for testing purposes but it also consumes disk space and many modules take time to load and also slows down PowerShell functionality and in that case Uninstall-Module helps to remove the module from the local and as well as the remote computers.
Recommended Articles
This is a guide to PowerShell uninstall module. Here we discuss How to use Uninstall module in PowerShell along with the examples and outputs. You may also have a look at the following articles to learn more –