PowerShell Modules Maintenance - Updated

Page content

Introduction

Almost a year ago, I wrote an article on this topic. Since it appeared in my search results for the same question again (that’s why I am blogging - not enough, to be honest), I took the opportunity to review the article and post an updated version with an improved script.

It’ll still update installed modules and remove older version in the C:\Users\<username>\Documents\PowerShell\Modules folder, but in a more userfriendly way :-)

Update June 30th, 2026

The script’s output has been improved and exception handling was implemented.

Update April 1st, 2026

I’ve discoverd an error, that removed updated modules instead of older versions, in case an update was performed.

PowerShell Script

By trusting the registry it is not necessary to confirm module updates every time. The script will iterate all installed modules, check each for updates, install them and remove older versions.

Write-Host 'Updating PowerShell modules...'

Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction Stop
$installedModules = Get-InstalledModule -ErrorAction Stop
$i = 0

foreach ($module in $installedModules) {
    try {
        $i++
        $latestModule = Find-Module -Name $module.Name -ErrorAction SilentlyContinue
        $latestVersion = if ($latestModule) { $latestModule.Version } else { 'Unknown' }
        Write-Host -NoNewline "Processing $i/$($installedModules.Count): $($module.Name) (Current $($module.Version), Latest $latestVersion)"

        if (-not $latestModule) {
            Write-Host ' - Skipped (not found in PSGallery).'
            continue
        }

        if ([version]$module.Version -ge [version]$latestModule.Version) {
            Write-Host ' - Already up to date.'
            continue
        }

        Write-Host ' - Updating...' -NoNewline
        Update-Module -Name $module.Name -ErrorAction Stop
        $installedVersions = Get-InstalledModule -Name $module.Name -AllVersions -ErrorAction Stop
        $versionToKeep = $installedVersions | Sort-Object Version -Descending | Select-Object -First 1 -ExpandProperty Version
        $versionsToRemove = $installedVersions | Where-Object { $_.Version -ne $versionToKeep }
        if ($versionsToRemove) {
            Write-Host -NoNewline " Removing $($versionsToRemove.Version -join ', ')"
            $versionsToRemove | ForEach-Object {
                Uninstall-Module -Name $_.Name -RequiredVersion $_.Version -Force -ErrorAction Stop
            }
        }

        Write-Host ' - Done.'
    } catch {
        Write-Host " - Failed: $($_.Exception.Message)"
    }
}

You can download the script here.

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

...
Processing 100/146: Az.Workloads (Current 2.0.0, Latest 2.0.0) - Already up to date.
Processing 101/146: Microsoft.Graph (Current 2.36.1, Latest 2.38.0) - Updating... Removing 2.36.1 - Done.
Processing 102/146: Microsoft.Graph.Applications (Current 2.36.1, Latest 2.38.0) - Updating...
...

Summary

Updating PowerShell modules and removing old version is still a manual task, but with a script the tasks can be automated.