Get reminded of outdated modules

The PowerShell Gallery is wonderful. Finally we have package management system in place, and installing and updating modules have never been easier. But you still have to remember to check for updated modules. What if you could be reminded of outdated modules every time you start PowerShell?

OutdatedModules

Well, it’s just a few lines of code in your profile script, and you will get a reminder of any outdated modules you have loaded every time you start the console (or ISE).

$loadedModules = Get-Module
foreach ($module in $loadedModules) {
    $foundModule = Find-Package -Name $module.Name -ErrorAction SilentlyContinue
    if ($foundModule) {
        if ($module.Version -lt $foundModule.Version) {
            Write-Host "$($module.Name): Installed version: $($module.Version.ToString()) - Newest version: " -NoNewline
            Write-Host "$($foundModule.Version.ToString())" -ForegroundColor Red
        }
    }
}

Remember to put this code after you load your third-party modules though.

4 comments

  1. Any particular reason for using Find-Package and not Find-Module?

    Also I would be inclined to use Get-InstalledModule as this lists all the modules that have been installed from the PowerShell Gallery.

    Like

    1. You know, I actually used Find-Module first, but I got prompted to install NuGet as Package Provider all the time, so I had to change it to Find-Package instead. But good tip on Get-InstalledModule.. hadn’t actually thought of using that one 🙂

      Like

  2. Get-InstalledModule doesn’t return anything for me. I ended up adding the following to my profile:
    $modules = Get-Module -ListAvailable | where {$_.ModuleType -eq ‘Script’}
    $counter = 0
    foreach ($module in $modules) {
    Write-Progress -Activity “Checking $($module.Name)” -status “$counter of $($modules.Count)” `
    -percentComplete ($counter / $modules.Count*100)
    $foundModule = Find-Package -Name $module.Name -ErrorAction SilentlyContinue
    if ($foundModule -and $module.Name -eq $foundModule.Name) {
    if ($module.Version -lt $foundModule.Version) {
    Write-Host “$($module.Name): Installed version: $($module.Version.ToString()) – Newest version: ” -NoNewline
    Write-Host “$($foundModule.Version.ToString()) updating…” -ForegroundColor Red
    Update-Module $foundModule.Name -Force
    }
    }
    $counter++
    Write-Progress -Activity “Checking $($module.Name)” -status “$counter of $($modules.Count)” `
    -percentComplete ($counter / $modules.Count*100) -Completed:($counter -eq $modules.Count)
    }

    Like

    1. Thanks for sharing Dirk. I think Get-InstalledModule only returns modules that have been installed from the PowerShell Gallery (or other providers?). I have been playing around with the code locally at my end as well, and I’m contemplating using Get-Module -ListAvailable as you have done.

      Like

Leave a Reply to Øyvind Kallstad Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s