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" } }