Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jhochwald
Last active October 15, 2022 21:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhochwald/87e878b07087cd743cecd647877f4abe to your computer and use it in GitHub Desktop.
Save jhochwald/87e878b07087cd743cecd647877f4abe to your computer and use it in GitHub Desktop.
Quick and dirty script that removes all older versions of all installed PowerShell Modules.
#Requires -RunAsAdministrator
#Requires -Version 3.0 -Modules PowerShellGet
<#
.SYNOPSIS
PowerShell Module maintenance
.DESCRIPTION
Quick and dirty script that removes all older versions of all installed PowerShell Modules.
.EXAMPLE
PS C:\> .\invoke-ModuleMaint.ps1
# Removes all old versions for all installed PowerShell Modules.
.NOTES
Why:
I do an automated update of my Modules, this process just updates straight to the latest and
greatest version of each installed module. I ended up with a bunch of older version for most
Modules, and I needed something to clean this up.
I found several stuff that does the same thing, but they all use "Get-InstalledModule" and the
performance of this command is terrible! I have to use "Uninstall-Module" that is slow enough,
so I needed something that runs faster on my system, where I have a lot of Modules installed.
Please note:
This will try to remove all older versions of all installed powerShell versions.
There might be issues with newer versions, so be aware of that.
There is no check, just a simple removal off all older versions.
Licence:
Copyright 2018 Joerg Hochwald <http://jhochwald.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#>
[CmdletBinding()]
param ()
begin
{
# Get all Modules with every Version that the system knows about.
$AllModules = (Get-Module -ListAvailable -Refresh)
}
process
{
# Now we initiate a loop over the information we have.
foreach ($SingleModule in $AllModules)
{
# Get the detailed information for the Module
$paramGetModule = @{
ListAvailable = $true
Name = $SingleModule.name
}
$SingleInstance = (Get-Module @paramGetModule)
# Do we have more than one installed version?
if ($SingleInstance -is [array])
{
# What is the latest and greatest?
$latest = (($SingleInstance | Sort-Object -Property Version -Descending)[0]).Version
# Now loop over all older versions
foreach ($VersionToRemove in $SingleInstance)
{
if (($VersionToRemove.Version -lt $latest))
{
try
{
# This is damn slow, but it is the safest way to do it!
$paramUninstallModule = @{
Name = $VersionToRemove.Name
RequiredVersion = $VersionToRemove.Version
Force = $true
ErrorAction = 'Stop'
WarningAction = 'SilentlyContinue'
Confirm = $false
}
$null = (Uninstall-Module @paramUninstallModule)
}
catch
{
# TODO: Check if we need something here. Or do we just want to catch it?
Write-Verbose -Message 'Whoops'
}
}
}
}
}
}
end
{
# TODO: Check if we need something here.
Write-Verbose -Message 'We are done, have a nice day!'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment