Managing Installed Modules (Part 2)

by Nov 20, 2020

Whenever you install new modules via Install-Module or update existing modules via Update-Module, new module versions are installed side-by-side.

If you always work with the latest versions of modules and do not need to access a specific older version, you might want to find outdated modules and remove them:

# get all installed modules as a hash table
# each key holds all versions of a given module
$list = Get-InstalledModule |
Get-InstalledModule -AllVersions |
Group-Object -Property Name -AsHashTable -AsString

# take all module names...
$list.Keys | 
  ForEach-Object {
    # dump all present versions...
    $list[$_] | 
    # sort by version descending (newest first)
    Sort-Object -Property Version -Descending | 
    # and skip newest, returning all other
    Select-Object -Skip 1
} |
# remove outdated (check whether you really don't need them anymore)
Uninstall-Module -WhatIf

Twitter This Tip! ReTweet this Tip!