Update-module needs flag to remove previous versions
When you run the Update-Module cmdlet there should be a flag to say update the module to the newest version and remove all previous versions. I understand the default being to leave existing versions, but if you DO really want to remove the older versions it becomes pretty painful when you start dealing with a suite of modules like AzureRM. Since each of the underlying modules are different versions it's not like you can easily just do a uninstall-module with a version restriction on all at the same time.

Wanted to update this thread with the current status and an approach that will allow uninstalling easily.
At this time we are not planning on implementing this feature. There are a large number of error conditions that can arise during an installation, and the general PS approach is not to add large numbers of parameters to support what are actually separate actions. In this case, the install and uninstall are separate.
The following command can be used (without the ending -Whatif) to clean up older versions of installed modules:
$Latest = Get-InstalledModule (modulename); Get-InstalledModule (modulename) -AllVersions | ? {$_.Version -ne $Latest.Version} | Uninstall-Module -WhatIf
6 comments
-
Anonymous commented
You can try this script to do what you requested:
-
Anonymous commented
J. Keith's script did not work for me. It also only deals with individual modules and not the library as a whole. So - I made it better by writing a script that works, and loops through the entire library of modules and removes old versions...
$Latest = Get-InstalledModule
foreach ($item in $Latest) {
Get-InstalledModule -Name $item.Name -AllVersions | Where {$_.Version -ne $item.Version} | Uninstall-Module -WhatIf
} -
Pavel Andreev commented
Use this script to uninstall all old modules:
$Latest = Get-InstalledModule
foreach ($module in $Latest) {
Get-InstalledModule $module.Name -AllVersions | ? {$_.Version -ne $module.Version} | Uninstall-Module -Verbose
} -
Anonymous commented
I'd argue that the verb "update" inherently means "install new version and remove old version." If that wasn't the case, why not just use `install-module` to get a new module version?
-
Ferdinand Kuiper commented
This would be a very nice option... Both actualy update with remove old versions and uninstall a rang of modules like AzureRM.
-
Mike Wood commented
That would be helpful. It would be really nice to have the flag on the update-module so that you don't have to run the second command to remove the older version, but if you just made it super simple to keep only the newest version (especially across groups of modules that deploy together like the Azure CmdLets) that would be fine.