Categories
HOW TO Microsoft PowerShell

HOW TO uninstall older versions of a PowerShell module installed

When using Windows PowerShell ISE it’s possible to import a PowerShell module, open the Command tool panel (from the menu Add-ons -> Command) and clicking Refresh, then have a list of the commands available.

It’s annoying that sometimes the same command appears multiple times, as the result of an installed PowerShell module that you updated.

Today’s example is from NavContainerHelper PowerShell module, searching in my pc for one of its commands I had multiple entries for the same command, as shown in the following picture:

Solution:
Run Windows PowerShell ISE as Administrator.
Copy and paste the following PowerShell script that retrieves the latest version of the desired module and then recursively uninstalls the previous versions installed.
Interesting to note, you can use the -WhatIf parameter at the end to simulate and preview the changes without doing them (remember to remove -WhatIf parameter when you want to apply the changes and to set the $ModuleName to the desired PowerShell module).

$ModuleName = 'navcontainerhelper';
$Latest = Get-InstalledModule $ModuleName; 
Get-InstalledModule $ModuleName -AllVersions | ? {$_.Version -ne $Latest.Version} | Uninstall-Module -WhatIf

To see the command list updated you need to restart Windows PowerShell ISE.

Did my HOW TO help you? Leave a reply.

One reply on “HOW TO uninstall older versions of a PowerShell module installed”

Great article, thanks! I adapted this a bit into a one-liner. It’s more dangerous because it runs for all modules, but that can be easily changed by adding -Name to the first command, plus you can run it with -WhatIf anyway. But using the PipelineVariable automatic parameter, you can do this in one shot and thought I would share:

“`
Get-InstalledModule -PipelineVariable latest | Get-InstalledModule -AllVersions | ? { $_.Version -ne $latest.Version } | Uninstall-Module -WhatIf
“`

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.