Skip to content

Instantly share code, notes, and snippets.

@jkavanagh58
Created August 22, 2019 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkavanagh58/4a715800c9b4fbf78d55ecfbcad3b32b to your computer and use it in GitHub Desktop.
Save jkavanagh58/4a715800c9b4fbf78d55ecfbcad3b32b to your computer and use it in GitHub Desktop.
<#
.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