-
Notifications
You must be signed in to change notification settings - Fork 15
/
Remove-ObsoleteModule.ps1
52 lines (42 loc) · 1.84 KB
/
Remove-ObsoleteModule.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Function Remove-ObsoleteModule {
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'None')]
Param (
[Parameter(ParameterSetName = 'byName')]
[string]$Name,
[Parameter(ParameterSetName = 'Exclude')]
[string[]]$Exclude
)
Begin {
[int]$mCnt = 0
Import-Module -Name 'PackageManagement' -Verbose:$false
If ([string]::IsNullOrEmpty($Name)) {
$moduleList = Get-InstalledModule -Verbose:$false
}
Else {
$moduleList = Get-InstalledModule -Verbose:$false -Name $Name
}
}
Process {
ForEach ($module in $moduleList) {
If ($moduleList.Count -gt 1) {
Write-Progress -Activity 'Checking Module Versions...' -Status $($module.Name) -PercentComplete ((100 / $moduleList.Count) * $mCnt++)
}
If ($Exclude -contains $($module.Name)) {
Write-Verbose -Message "Skipping requested module: $($module.Name)"
Continue
}
[object[]]$specificModule = (Get-InstalledModule -Name $($module.name) -AllVersions -Verbose:$false | Sort-Object -Property Version)
[object] $latestVersion = $specificModule[-1]
Write-Verbose -Message "Found $($specificModule.Count) version(s) for $($module.Name)"
ForEach ($specific in $specificModule) {
If ($specific.Version -eq $latestVersion.Version) { Continue }
If ($PSCmdlet.ShouldProcess($($specific.Name), "Uninstall version '$($specific.version)'")) {
Write-Output -InputObject "Uninstalling version '$($specific.version)' of $($specific.Name)"
$specific | Uninstall-Module -force
}
}
}
}
End {
}
}