Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active May 30, 2023 11:17
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdhitsolutions/8a49a59c5dd19da9dde6051b3e58d2d0 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/8a49a59c5dd19da9dde6051b3e58d2d0 to your computer and use it in GitHub Desktop.
Test installed PowerShell modules against online versions in the PowerShell Gallery.
[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 -as [version]) -gt ($module.version -as [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"
@jdhitsolutions
Copy link
Author

@roysubs
Copy link

roysubs commented Dec 20, 2020

Really useful script. Can be nice to add Param($mods), then add $mods at the end of the $modules = Get-Module -ListAvailable line, to then be able to focus the script on an individual Module or set of Modules as an option.

@jdhitsolutions
Copy link
Author

I had completely forgotten about this script. Posted a revision with better error handling and verbose output.

@jdhitsolutions
Copy link
Author

This script can be installed from the PowerShell Gallery:

Install-Script Check-ModuleUpdate

@ntpeters
Copy link

ntpeters commented Aug 26, 2021

@jdhitsolutions there's a bug in the version comparison here. Since this is just comparing the raw strings, it doesn't take into account semantic versioning.
For example:

"1.13.0" -gt "1.7.1" == $false

This should evaluate to true, and can be resolved correctly by casting these to System.Version first:

[System.Version]"1.13.0" -gt [System.Version]"1.7.1" == $true

As a side note, it'd also be great to have a flag to only return modules that have updates (eg. something like -Outdated).

@jdhitsolutions
Copy link
Author

You caught me being lazy. Good catch. I've updated the script. And the output includes a boolean property Update that you can use. If the value is True that means it is outdated and that an update is available.

c:\scripts\check-moduleupdate.ps1 | where Update | update-module

@jdhitsolutions
Copy link
Author

The PSScriptTools module has an updated version of this script which you can use as a function. The function properly compares version info.

@franklesniak
Copy link

Hey Jeff, where in the PSScriptTools module is this function? I might be being dense, but I don't see it.

@jdhitsolutions
Copy link
Author

The command in the PSScriptTools module is called Compare-Module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment