Check-ModuleUpdate.ps1

#requires -version 5.0

<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID 7da2acc6-30d8-4cc9-a3d9-ba645fceebb2
 
.AUTHOR Jeff Hicks
 
.COMPANYNAME
 
.COPYRIGHT 2017
 
.TAGS PowerShellget Module PSGallery
 
.LICENSEURI
 
.PROJECTURI https://gist.github.com/jdhitsolutions/8a49a59c5dd19da9dde6051b3e58d2d0
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
This code is described at http://jdhitsolutions.com/blog/powershell/5441/check-for-module-updates/
 
#>


<#
.DESCRIPTION
 Check for module updates from the PowerShell gallery and create a comparison object
 
#>
 

[cmdletbinding()]
Param()

Write-Host "Getting installed modules" -ForegroundColor Yellow
$modules = Get-Module -ListAvailable

#group to identify modules with multiple versions installed
$g = $modules | group-object name -NoElement | where-object count -gt 1

Write-Host "Filter to modules from the PSGallery" -ForegroundColor Yellow
$gallery = $modules.where({$_.repositorysourcelocation})

Write-Host "Comparing to online versions" -ForegroundColor Yellow
foreach ($module in $gallery) {

     #find the current version in the gallery
     Try {
        $online = Find-Module -Name $module.name -Repository PSGallery -ErrorAction Stop
     }
     Catch {
        Write-Warning "Module $($module.name) was not found in the PSGallery"
     }

     #compare versions
     if ($online.version -gt $module.version) {
        $UpdateAvailable = $True
     }
     else {
        $UpdateAvailable = $False
     }

     #write a custom object to the pipeline
     [pscustomobject]@{
        Name = $module.name
        MultipleVersions = ($g.name -contains $module.name)
        InstalledVersion = $module.version
        OnlineVersion = $online.version
        Update = $UpdateAvailable
        Path = $module.modulebase
     }
 
} #foreach


Write-Host "Check complete" -ForegroundColor Green