Cleanup functionality, where only the latest version of a module is kept
Summary of the new feature / enhancement
As a user/administrator I want to only keep the latest version of any module, so I don't accumulate many outdated versions on the systems that I manage.
Proposed technical implementation details (optional)
Functional implementation ideas
- Update-PsResource -KeepLatestOnly
- Uninstall-PsResource -KeepLatest
Technical idea The GetHelper class could be used and expanded to implement a filter returning all modules except the latest version. With that, the proper uninstalls could be performed (option 2) after installing the newest version (option 1).
vote++
One issue here is you're going to need to check all existing modules for their requiredmodules and preserve any pinned versions as well, otherwise you're going to break those modules with this "cleanup". In addition, how are you going to know that a script doesn't require a pinned module that, upon doing this, will break that script?
One issue here is you're going to need to check all existing modules for their requiredmodules and preserve any pinned versions as well, otherwise you're going to break those modules with this "cleanup". In addition, how are you going to know that a script doesn't require a pinned module that, upon doing this, will break that script?
Great point @JustinGrote!
A rudimentary CheckIfDependency method is already in place, in the Uninstall-PsResource cmdlet, and probably needs to be expanded upon.
The comment there suggests a database of installed PsResources, so a dependency check can be done quickly.
I'm fine with that risk, as long as the parameter is optional and comes with a warning.
Related issue in regards to the DSC resource but still the same feature request: https://github.com/PowerShell/PowerShellGetDsc/issues/7
One issue here is you're going to need to check all existing modules for their requiredmodules and preserve any pinned versions as well, otherwise you're going to break those modules with this "cleanup". In addition, how are you going to know that a script doesn't require a pinned module that, upon doing this, will break that script?
Great point @JustinGrote! A rudimentary CheckIfDependency method is already in place, in the
Uninstall-PsResourcecmdlet, and probably needs to be expanded upon. The comment there suggests a database of installed PsResources, so a dependency check can be done quickly.
That method would also need to take into account installed script PSResources that have module dependencies.
This brings up an age-old dilemma of whether the platform should police programmer habits, or if programmers should assume the responsibility of identifying dependencies properly. If there's a mechanism in place (or forthcoming) for developers to indicate dependencies, including version-level, then the developers should handle that risk. And even if I break another module (or script) by removing a dependent version, I can simply reinstall it.
I think having this functionality with a confirm that dependencies are not (or not fully) taken into account would be a fine place to start from. Even if such a dependency management system is not implemented.
I will provide examples from various places that prove there is interest in this functionality:
- http://sharepointjack.com/2017/powershell-script-to-remove-duplicate-old-modules/
- https://4bes.nl/2021/09/19/update-all-powershell-modules-on-a-system/
- https://github.com/yubu/ps.checkModuleUpdates/blob/08a51c7f95e39e7f34f422ce4e76a4dfd7185a24/ps.checkModuleUpdates.psm1#L68
- https://github.com/microsoft/PFE/blob/b57a97d7c466d11a34420eeddd4af01e0f05c5a5/PowerShell/Scripts/AutoModuleInstallAndUpdate/AutoModuleInstallAndUpdate.ps1#L343
- https://github.com/null-lex-null/posh/blob/4bd002fb631add4bafa56a986490248f54ba4364/apps/CleanupPackages.ps1
- https://jawz84.wordpress.com/2018/08/22/how-to-clean-up-older-versions-of-installed-powershell-modules/ (yes, found I blogged about this myself, some years ago)
None of these solutions provide much dependency checking. Ofcourse it would be super cool if there was full dependency management, but in my opinion that can be implemented later.
| Assert-Duplicate -Near -Fuzzy $_ PowerShell/PowerShellGet#86