Last active
May 30, 2023 11:17
-
-
Save jdhitsolutions/8a49a59c5dd19da9dde6051b3e58d2d0 to your computer and use it in GitHub Desktop.
Test installed PowerShell modules against online versions in the PowerShell Gallery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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" |
The PSScriptTools module has an updated version of this script which you can use as a function. The function properly compares version info.
Hey Jeff, where in the PSScriptTools module is this function? I might be being dense, but I don't see it.
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
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