If you’ve installed modules in powershell such as the AzureRM collection, and have updated to newer versions of said modules, it’s possible that you might have both the old and new versions installed. This can slow down some commands in powershell (get-command for example)
I wrote a small script to remove old versions of Powershell modules using what I found at https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/14934876-update-module-needs-flag-to-remove-previous-versio) as a starter.
Note that when I say “Remove old versions” I mean it leaves the most current one you have and removes the older ones – if your system is 3 versions behind, it will not go fetch new commands first (though it does remind you of the command to do that)
write-host "this will remove all old versions of installed modules" write-host "be sure to run this as an admin" -foregroundcolor yellow write-host "(You can update all your Azure RM modules with update-module Azurerm -force)" $mods = get-installedmodule foreach ($Mod in $mods) { write-host "Checking $($mod.name)" $latest = get-installedmodule $mod.name $specificmods = get-installedmodule $mod.name -allversions write-host "$($specificmods.count) versions of this module found [ $($mod.name) ]" foreach ($sm in $specificmods) { if ($sm.version -ne $latest.version) { write-host "uninstalling $($sm.name) - $($sm.version) [latest is $($latest.version)]" $sm | uninstall-module -force write-host "done uninstalling $($sm.name) - $($sm.version)" write-host " --------" } } write-host "------------------------" } write-host "done"
Hope this helps!
Update 3-2018:
The script above was originally missing a -force parameter, this has been fixed.
While troubleshooting the script above, I wanted a fast, non-destructive way to see what modules I had installed that had multiple versions.
I created the script below for that purpose, you may find it useful:
write-host "this will report all modules with duplicate (older and newer) versions installed" write-host "be sure to run this as an admin" -foregroundcolor yellow write-host "(You can update all your Azure RMmodules with update-module Azurerm -force)" $mods = get-installedmodule foreach ($Mod in $mods) { write-host "Checking $($mod.name)" $latest = get-installedmodule $mod.name $specificmods = get-installedmodule $mod.name -allversions write-host "$($specificmods.count) versions of this module found [ $($mod.name) ]" foreach ($sm in $specificmods) { if ($sm.version -eq $latest.version) { $color = "green"} else { $color = "magenta"} write-host " $($sm.name) - $($sm.version) [highest installed is $($latest.version)]" -foregroundcolor $color } write-host "------------------------" } write-host "done"
- Jack