Skip to content

Instantly share code, notes, and snippets.

@TangChr
Last active April 29, 2021 09:35
Show Gist options
  • Save TangChr/849fd1efbd663aae5038e927df2608d2 to your computer and use it in GitHub Desktop.
Save TangChr/849fd1efbd663aae5038e927df2608d2 to your computer and use it in GitHub Desktop.
PowerShell method to remove old versions of installed modules
function Remove-OldModules
{
Write-Prompt "------------------------------------------" -ForegroundColor Cyan
Write-Prompt "Removing old versions of installed modules" -ForegroundColor Cyan
Write-Prompt "------------------------------------------" -ForegroundColor Cyan
$modules = Get-InstalledModule
Write-Prompt "$($modules.count) module(s) found." -ForegroundColor Yellow
Write-Prompt ""
foreach ($module in $modules)
{
$latest = Get-InstalledModule $module.name
$installed = Get-InstalledModule $module.name -AllVersions
Write-Prompt "$($installed.count) version(s) of this module found [$($module.name)] - Latest is $($latest.version)"
foreach ($installedModule in $installed)
{
if ($installedModule.version -ne $latest.version)
{
Write-Prompt "Uninstalling $($installedModule.name) - $($installedModule.version)"
$installedModule | Uninstall-Module -Force
}
}
Write-Prompt "---------------------" -ForegroundColor Cyan
}
$prompt = Write-Prompt "Done removing old versions of installed modules" -ForegroundColor Cyan
$prompt += "`n"
$prompt += Write-Prompt "-----------------------------------------------" -ForegroundColor Cyan
return $prompt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment