Skip to content

Instantly share code, notes, and snippets.

@JeffMill
Last active March 25, 2025 18:57
Show Gist options
  • Save JeffMill/3dabcb0aa2eb4b04fb533b27f782cd8c to your computer and use it in GitHub Desktop.
Save JeffMill/3dabcb0aa2eb4b04fb533b27f782cd8c to your computer and use it in GitHub Desktop.
Remove older PowerShell Az modules
# Note: This doesn't always work correctly.
# might get: Uninstall-Package: No match was found for the specified search criteria and module names...
# Can remove all az.* packages (and then manually reinstall) using:
# $env:PSModulePath -split ';' | ForEach-Object { Get-ChildItem -Directory -Path (Join-Path $_ 'Az.*') | ForEach-Object { Remove-Item -LiteralPath $_.FullName -Recurse -Verbose -Force } }
# (Which could also be updated to remove just older versions in each folder)
foreach ($module in (Get-Module -ListAvailable -Name 'Az*').Name | Get-Unique) {
$modules = Get-Module -ListAvailable $module
if ($modules.Count -gt 1) {
$Latest_Version = ($modules | Select-Object Version | Sort-Object Version)[-1]
'Module {0} latest version is {1}' -f $module, $Latest_Version.Version
$modules | Where-Object Version -ne $Latest_Version.Version | ForEach-Object {
'Removing older module {0} {1}' -f $_.Name, $_.Version
Uninstall-Module -Name $_.Name -RequiredVersion $_.Version -Verbose
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment