-
-
Save jdhitsolutions/8a49a59c5dd19da9dde6051b3e58d2d0 to your computer and use it in GitHub Desktop.
[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" |
This script can be installed from the PowerShell Gallery:
Install-Script Check-ModuleUpdate
@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
).
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
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
.
I had completely forgotten about this script. Posted a revision with better error handling and verbose output.