Created
February 27, 2023 16:03
-
-
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
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
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