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.