Last active
April 29, 2021 09:35
-
-
Save TangChr/849fd1efbd663aae5038e927df2608d2 to your computer and use it in GitHub Desktop.
PowerShell method to remove old versions of installed modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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