Back
Featured image of post Quick and dirty script that removes all older versions of all installed PowerShell Modules.

Quick and dirty script that removes all older versions of all installed PowerShell Modules.

I use a lot of PowerShell Modules. Some of them are deployed via a Build Server, so I get a lot of test builds! All of them have a new version tag, which is good. And I also use a lot of Modules from the PowerShell Gallery; some of them are updated frequently.

So my Systems ended up with a lot of older versions for several modules.

Manually removal is something I don’t like… So I started to search for something that removes all older versions at once, so my system has just the latest and greatest version of each Module left.

I found several solutions, but all of them seems to use "

Get-InstalledModule" that is good but also very, very, slow. And I mean very slow if you have a lot of Modules installed.

So I came up with the following:

#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!'
}

There is also a Gist for that.

You might find this useful. However, please keep in mind, that this script is just a straight-forward approach with no real error handling or logging. It will remove all older versions for really all installed PowerShell modules. At least, it tries to uninstall them. If something went wrong (An error is caught, the script will just continue).