plugins/moduleclean/src/Remove-OldModule.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#Requires -Version 5
function Global:Remove-OldModule {
    <#
    .SYNOPSIS
        A small wrapper for PowerShellGet to remove all older installed modules.
    .DESCRIPTION
        A small wrapper for PowerShellGet to remove all older installed modules.
    .PARAMETER ModuleName
        Name of a module to check and remove old versions of. Default is all modules ('*')
    .PARAMETER Silent
        Do not show progress bar.
    .PARAMETER Force
        Force removal without any confirmation prompts.
    .EXAMPLE
        PS> Remove-OldModules
 
        Removes old modules installed via PowerShellGet.
 
    .EXAMPLE
        PS> Remove-OldModules -whatif
 
        Shows what old modules might be removed via PowerShellGet.
 
    .NOTES
        Author: Zachary Loeber
        Site: http://www.the-little-things.net/
 
 
        Version History
        1.0.0 - Initial release
    #>

    [CmdletBinding( SupportsShouldProcess=$True, ConfirmImpact='Medium' )]
    Param (
        [Parameter(HelpMessage = 'Name of a module to check and remove old versions of.')]
        [string]$ModuleName = '*',
        [Parameter(HelpMessage = 'Force upgrade modules without confirmation.')]
        [Switch]$Force,
        [Parameter(HelpMessage = 'Do not write progress.')]
        [Switch]$Silent
    )

    Begin {
        try {
            Import-Module PowerShellGet
        }
        catch {
            Write-Warning 'Unable to load PowerShellGet. This script only works with PowerShell 5 and greater.'
            return
        }

        $YesToAll = $false
        $NoToAll = $false

        $ModulesToRemove = @()
    }

    Process {
        $Count = 0

        Get-InstalledModule $ModuleName | ForEach-Object {
            $Count ++
            if (-not $Silent) {
                Write-Progress -Activity "Calculating removable modules" -PercentComplete ($Count % 100) -Status "Calculating"
            }
            $ThisModule = Get-InstalledModule $_.Name -AllVersions | Sort-Object Version
            If ($ThisModule.count -gt 1) {
                $ModulesToRemove += $ThisModule | Select-Object -First ($ThisModule.count - 1)
            }
        }

        $Count = 0
        $TotalMods = $ModulesToRemove.Count
        ForEach ($Mod in $ModulesToRemove) {
            $Ver = $Mod.Version.ToString()
            $Count++

            if ($pscmdlet.ShouldProcess("Remove module $($Mod.Name) - $($Ver)", 
            "Remove module $($Mod.Name) - $($Ver)?",
            "Removing module $($Mod.Name) - $($Ver)")) {
                if($Force -Or $PSCmdlet.ShouldContinue("Are you REALLY sure you want to remove '$($Mod.Name) - $($Ver) '?",
                "Removing module '$($Mod.Name) - $($Ver)'",
                [ref]$YesToAll,
                [ref]$NotoAll)) {
                    if (-not $Silent) {
                        $PercentComplete = [math]::Round((100*($Count/$TotalMods)),0)
                        Write-Progress -Activity "Removing Old Module $($Mod.Name) (version: $($Ver))" -PercentComplete $PercentComplete -Status "Removing..."
                    }
                    Uninstall-Module $Mod.Name -RequiredVersion $Ver -Force -Confirm:$false
                }
            }
        }
    }
}