PowerShell Modules Maintenance

Page content

Introduction

From time-to-time I am updating all installed PowerShell modules on my dev-machines. This can lead to a large amount of used disk space, as old versions are kept on the drive. In this post, I want to share the scripts for the two tasks. After updating modules, I noticed a large amount of data is stored in my C:\Users\<username>\Documents\PowerShell\Modules folder. To be specific, the folder eats up 3.1GB of space. Therefore I needed a cleanup script as well :-)

PowerShell Script

I don’t like to use the -force parameter installing modules. It sometimes hides essential information. You can simply enter A once, to accept app updates.

# Update all installed PowerShell modules
Get-InstalledModule | Update-Module
# uninstall old versions to cleanup space
foreach ($module in Get-InstalledModule) { 
  Write-Verbose -Message "Removing old versions of the module $($module.Name)" -Verbose
  Get-InstalledModule -Name $module.Name -AllVersions | Where-Object { $_.Version -ne $module.Version } | Uninstall-Module -Verbose -force
}

It’ll take a while to run the cleanup script, depending on the amount of installed modules and version.

Summary

Working with the latest versions of modules helps to avoid the “it works on my machine” support questions. But keep in mind that cleaning up old version might also make sense, as old versions keep filling your harddisk. After running the script, the used space of the folder went down from 3.1 GB to 1.8 GB.