Skip to content

Instantly share code, notes, and snippets.

@kkamegawa
Last active June 9, 2025 04:09
Show Gist options
  • Save kkamegawa/0ae150f4e5985a9929aabfc4b7c11be9 to your computer and use it in GitHub Desktop.
Save kkamegawa/0ae150f4e5985a9929aabfc4b7c11be9 to your computer and use it in GitHub Desktop.
remove unnecessary old PowerShell modules (Azure, Microsoft.Entra, Microsoft.Graph)
import-module -name PoshSemanticVersion
function Get-LatestVersion
(
[Parameter(Mandatory=$true)]
[string]$moduleName
)
{
$allversions = Get-InstalledModule -Name $moduleName | Sort-Object -Property Version
for($i = 0; $i -lt $allversions.Count; $i++){
for($j = $i; $j -lt $allversions.Count; $j++){
$result = Compare-SemanticVersion -ReferenceVersion $allversions[$i].Version -DifferenceVersion $allversions[$j].Version
if($result.Precedence -eq '>'){
$temp = $allversions[$i]
$allversions[$i] = $allversions[$j]
$allversions[$j] = $temp
}
}
}
return $allversions[$allversions.Count - 1].Version
}
function Remove-OldModule
(
[Parameter(Mandatory=$true)]
[string]$moduleName
)
{
# get all versions of the module
# but it can't get all versions of the module
$allversions = Get-InstalledModule -Name $moduleName -AllVersions
if ($allversions.count -eq 1) {
return
}
# compare all versions of the module with semantic version
for($i = 0; $i -lt $allversions.Count; $i++){
for($j = $i; $j -lt $allversions.Count; $j++){
$result = Compare-SemanticVersion -ReferenceVersion $allversions[$i].Version -DifferenceVersion $allversions[$j].Version
if($result.Precedence -eq '>'){
$temp = $allversions[$i]
$allversions[$i] = $allversions[$j]
$allversions[$j] = $temp
}
}
} for ($i = 0; $i -lt $allversions.Count - 1; $i++) {
write-host "remove:" $moduleName $allversions[$i].Version
Uninstall-Module -Name $moduleName -RequiredVersion $allversions[$i].Version -Force
}
}
# Define module patterns to clean up
$modulePatterns = @('az*', 'Microsoft.Entra*', 'Microsoft.Graph*')
foreach($pattern in $modulePatterns) {
write-host "Processing modules matching pattern: $pattern"
$installedmodules = Get-InstalledModule -Name $pattern -ErrorAction SilentlyContinue | select-object Name
if ($installedmodules) {
foreach($module in $installedmodules){
write-host "find module:" $module.Name
Remove-OldModule -moduleName $module.Name
}
} else {
write-host "No modules found matching pattern: $pattern"
}
}
# remove unused folder in module path
$documentPath = [System.Environment]::GetFolderPath('mydocuments')
$moduleTargetPath = join-path -Path $documentPath -ChildPath 'PowerShell' 'Modules'
# Clean up unused folders for all module patterns
$modulePatterns = @('az*', 'Microsoft.Entra*', 'Microsoft.Graph*')
foreach($pattern in $modulePatterns) {
write-host "Cleaning up folders for modules matching pattern: $pattern"
$installedmodules = Get-InstalledModule -Name $pattern -ErrorAction SilentlyContinue | select-object Name
if ($installedmodules) {
foreach($module in $installedmodules){
$moduleVersion = Get-LatestVersion -moduleName $module.Name
$modulePath = join-path -Path $moduleTargetPath -ChildPath $module.Name
if (Test-Path $modulePath) {
$items = Get-childitem $modulePath | Where-Object { $_.Name -ne $moduleVersion }
foreach($item in $items)
{
write-host "removed folder contents:" $module.Name $item.Name
Remove-Item -Path $item.FullName -Recurse -Force
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment