Skip to content

Instantly share code, notes, and snippets.

@oliverlabs
Created February 27, 2023 16:03
Show Gist options
  • Save oliverlabs/1688fd56175c01f7a59252a95f118ce0 to your computer and use it in GitHub Desktop.
Save oliverlabs/1688fd56175c01f7a59252a95f118ce0 to your computer and use it in GitHub Desktop.
A tiny script to remove older versions of the Az module in PowerShell
foreach ($module in (Get-Module -ListAvailable Az*).Name |Get-Unique) {
if ((Get-Module -ListAvailable $module).Count -gt 1) {
$Latest_Version = (Get-Module -ListAvailable $module | select Version | Sort-Object Version)[-1]
write-host "The latest $module version is $Latest_Version"
Get-Module -ListAvailable $module | Where-Object {$_.Version -ne $Latest_Version.Version} | foreach {Uninstall-Module -Name $_.Name -RequiredVersion $_.Version -verbose}
}
else {
Write-Output "Only one module version exists for the $module"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment