Created
August 22, 2019 15:54
-
-
Save jkavanagh58/4a715800c9b4fbf78d55ecfbcad3b32b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<# | |
.SYNOPSIS | |
Clean up PowerShell Installed Modules | |
.DESCRIPTION | |
Examine installed modules and determine versions that can be removed from installation folder. | |
.PARAMETER modInstalled | |
Collection of currently installed modules installed via PowerShellGet | |
.EXAMPLE | |
PS> ./uninstall-module-history.ps1 | |
Example of how to use this script | |
.NOTES | |
=========================================================================== | |
Created with: Visual Studio Code | |
Created on: 08.22.2019 | |
Created by: John J. Kavanagh | |
Organization: KavanaghTech | |
Filename: uninstall-modulehistory.ps1 | |
=========================================================================== | |
08.22.2019 JJK: TODO: clean-up progress bars | |
#> | |
[CmdletBinding()] | |
Param( | |
[parameter(Mandatory=$False, ValueFromPipeline=$False, | |
HelpMessage = "Collection of locally installed modules")] | |
[System.Array]$modInstalled | |
) | |
BEGIN { | |
"Gathering list of Installed Modules" | |
$modInstalled = Get-InstalledModule | |
$i = 0 | |
} | |
PROCESS { | |
$writeProgressSplat = @{ | |
Id = 1 | |
Activity = "Module Version clean up for $($modInstalled.Count) modules" | |
} | |
Write-Progress @writeProgressSplat | |
ForEach ($curModule in $modInstalled){ | |
$i++ | |
$writeProgressSplat = @{ | |
PercentComplete = (($i / $modInstalled.Count) * 100) | |
Activity = "Checking $($curModule.Name)" | |
Id = 2 | |
ParentId = 1 | |
} | |
Write-Progress @writeProgressSplat | |
$modInfo = Get-InstalledModule $curModule.Name -AllVersions | |
"{0} has an installed history of {1} versions, Latest version is {2}" -f $curModule.Name, $modinfo.count, $curModule.Version | |
$oldVersions = $modinfo | Where-Object {$_.Version -ne $curModule.Version} | |
If ($oldVersions){ | |
ForEach ($deprecated in $oldversions) { | |
Try { | |
$writeProgressSplat = @{ | |
Activity = "Uninstalling $($deprecated.Name) Version $($deprecated.Version)" | |
Id = 3 | |
ParentId = 2 | |
} | |
Write-Progress @writeProgressSplat | |
$deprecated | Uninstall-Module -Confirm:$false -Force | |
"`tUninstalled Version: {0}" -f $deprecated.Version | |
} | |
Catch { | |
"`tUnable to Uninstall Version: {0}" -f $deprecated.Version | |
$error[0].Exception.Message | |
} | |
} | |
} | |
Else { | |
"`tNo Clean-up needed for {0}" -f $curModule.Name | |
} | |
} | |
} | |
END { | |
Remove-Variable -Name curModule, modInstalled, modInfo, i | |
[System.GC]::Collect() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment