Check-ModuleUpdate.ps1

#requires -version 5.0

<#PSScriptInfo
 
.VERSION 2.0.0
 
.GUID 7da2acc6-30d8-4cc9-a3d9-ba645fceebb2
 
.AUTHOR Jeff Hicks
 
.COMPANYNAME JDH Information Technology Solutions, Inc.
 
.COPYRIGHT (c) 2017-2021 JDH Information Technology Solutions, Inc.
 
.TAGS PowerShellget Module PSGallery
 
.LICENSEURI https://gist.github.com/jdhitsolutions/d97a81b86ca7a8f419f75acd33368ece
 
.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()]
[outputtype("moduleInfo")]
Param(
   [Parameter(Position = 0, HelpMessage = "Enter a module name or names. Wildcards are allowed.")]
   [ValidateNotNullorEmpty()]
   [string[]]$Name = "*"
)

Write-Verbose "Getting installed modules"
Try {
   $modules = Get-Module -Name $name -ListAvailable -ErrorAction Stop
}
Catch {
   Throw $_
}

if ($modules) {
   Write-Verbose "Found $($modules.count) matching modules"
   #group to identify modules with multiple versions installed
   Write-Verbose "Grouping modules"
   $g = $modules | Group-Object name -NoElement | Where-Object count -GT 1

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

   Write-Verbose "Comparing to online versions"
   foreach ($module in $gallery) {

      #find the current version in the gallery
      Try {
         Write-Verbose "Looking online for $($module.name)"
         $online = Find-Module -Name $module.name -Repository PSGallery -ErrorAction Stop
         #compare versions
         if ($online.version -gt $module.version) {
            $UpdateAvailable = $True
         }
         else {
            $UpdateAvailable = $False
         }

         #write a custom object to the pipeline
         [pscustomobject]@{
            PSTypeName       = "moduleInfo"
            Name             = $module.name
            MultipleVersions = ($g.name -contains $module.name)
            InstalledVersion = $module.version
            OnlineVersion    = $online.version
            Update           = $UpdateAvailable
            Path             = $module.modulebase
         }
      }
      Catch {
         Write-Warning "Module $($module.name) was not found in the PSGallery"
      }

   } #foreach
}
else {
   Write-Warning "No matching modules found."
}

Write-Verbose "Check complete"