Created
July 26, 2019 17:35
-
-
Save JustinGrote/ee05ed0fa07f7fc47a2d7a1a809954ac to your computer and use it in GitHub Desktop.
Quickly remove old duplicate modules within each module-specific folder. Supports -whatif and confirms by default
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
#requires -version 5 | |
function Remove-OldModule { | |
[CmdletBinding(SupportsShouldProcess,ConfirmImpact="High")] | |
param ( | |
#Include deleting old versions of modules that might "ship" with modules | |
[Switch]$IncludeSystemProvidedModules, | |
#Exclude Specified modules. Must match the module name exactly | |
[String[]]$Exclude | |
) | |
#ModuleBase is used so that only duplicates within the same modules folder are removed. This avoids deleting your system PSReadline if you have a user module psreadline, for instance. | |
$candidateModules = gmo -listavailable | where name -notin $Exclude | select name, version, modulebase, @{N = "moduledir"; E = { (Get-Item $PSItem.modulebase).parent.parent.fullname } } | group name,moduledir | where count -gt 1 | |
foreach ($moduleItem in ($candidateModules)) { | |
$latestmodule = $moduleItem.group | sort version -descending | select -first 1 | |
Write-Host -fore Green "Removing Old versions of $($latestmodule.Name). Latest Version: $($latestmodule.Version)" | |
foreach ($moduleToRemoveItem in ($moduleItem.group | sort version -descending | select -skip 1)) { | |
if ($PSCmdlet.ShouldProcess($moduleToRemoveItem.ModuleBase)) { | |
Remove-Item -Recurse -Force $moduleToRemoveItem.modulebase | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment