Simple script to remove old PowerShell modules

If your like me, you have collections of PowerShell modules that just keep accumulating after every update, since they don’t clean up after themselves. This script will traverse all your modules and remove all but the latest version. It’s a lifesaver!

Get-InstalledModule | ForEach-Object {
    $latestVersion = $PSItem.Version

    Write-Host "$($PSItem.Name) - $($PSItem.Version)"

    Get-InstalledModule $PSItem.Name -AllVersions |
    Where-Object Version -NE $latestVersion |
    ForEach-Object {
        Write-Host "- Uninstalling version $($PSItem.Version)..." -ForegroundColor Yellow -NoNewline
        $PSItem | Uninstall-Module -Force
        Write-Host "done"
    }
}

 

Leave a Reply

Your email address will not be published.

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