Quickly Finding Outdated PowerShell Modules

by Jun 27, 2022

In the most simplistic case, you can check all your installed modules for updates with just a one-liner (remove -WhatIf to actually perform the update):

 
PS C:\> Get-InstalledModule | Update-Module -WhatIf  

Get-InstalledModule lists all modules that were installed in a “managed” way (using Install-Module) and includes information about where the module was installed from (i.e. PowerShell Gallery website). That’s the information Update-Module needs to be able to check for new versions.

If you’re just interested to see whether any of your modules need an update, and if you are focused on modules installed from the PowerShell Gallery only, then here’s a much faster way to check for updates:

function Test-GalleryModuleUpdate
{
    param
    (
        [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
        [string]
        $Name,

        [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
        [version]
        $Version,

        [switch]
        $NeedUpdateOnly

    )
    
    process
    {
        $URL = "https://www.powershellgallery.com/packages/$Name" 
        $page = Invoke-WebRequest -Uri $URL -UseBasicParsing -Maximum 0 -ea Ignore
        [version]$latest  = Split-Path -Path $page.Headers.Location -Leaf
        $needsupdate = $Latest -gt $Version

        if ($needsupdate -or (!$NeedUpdateOnly.IsPresent))
        {
            [PSCustomObject]@{
                ModuleName = $Name
                CurrentVersion = $Version
                LatestVersion = $Latest
                NeedsUpdate = $needsupdate
            }
        }
    }
}

Get-InstalledModule | Where-Object Repository -eq PSGallery | 
  Test-GalleryModuleUpdate #-NeedUpdateOnly

The function Test-GalleryModuleUpdate takes the modules emitted by Get-InstalledModule and checks whether there is a new version published at powershellgallery.com. This check is done by URL resolution in a very fast way. If you add the -NeedUpdateOnly switch parameter, then Test-GalleryModuleUpdate returns only modules that need updates (may be none).

Here is a sample output:

 
ModuleName    CurrentVersion LatestVersion NeedsUpdate
----------    -------------- ------------- -----------
ImportExcel   7.5.2          7.5.3                True
PSEventViewer 1.0.17         1.0.22               True
Az.Tools.P... 0.5.0          1.0.1                True
Microsoft.... 16.0.21116.... 16.0.22413...        True
MicrosoftT... 2.3.1          4.4.1                True
PSReadLine    2.2.2          2.2.5                True
PSWriteHTML   0.0.172        0.0.174              True 
...  

 


Twitter This Tip! ReTweet this Tip!