Keeping Your Modules Up-To-Date

by Apr 29, 2016

Once you start downloading modules from the PowerShell Gallery (or via PowerShellGet in general), you get version control automatically. You can always review the modules that were installed, and compare their versions with the versions available in the repository.

This way, it's easy to find out the modules that need an update. The following code assumes that you know the PowerShell Gallery already. If not, visit powershellgallery.com.

Get-InstalledModule | ForEach-Object {
    try
    {
        $module = Find-Module -Name $_.Name -ErrorAction Stop
        $newVersion = $module.Version
        $needsUpdate = $_.Version -lt $newVersion
    }
    catch
    {
        $newVersion = 'no longer available'
        $needsUpdate = $true
    }
    $_ | Add-Member -MemberType NoteProperty -Name VersionAvailable $newVersion 
    $_ | Add-Member -MemberType NoteProperty -Name NeedsUpdate $needsUpdate 
    $_
  } |
  Select-Object -Property Name, NeedsUpdate, Version, VersionAvailable |
  Out-GridView

When you run this code, you get a list of modules that were installed via PowerShellGet, and version information that tells whether a module needs an update. The output looks similar to this:

 
Name                   NeedsUpdate Version  VersionAvailable   
----                   ----------- -------  ----------------   
Pester                       False 3.4.0    3.4.0              
xDefender                    False 0.2.0.0  0.2.0.0            
xRobocopy                     True 1.1.0.0  1.2.0.0            
xWebAdministration            True 1.8.0.0  1.9.0.0            
CommunityAnalyzerRules        True 0.0.0.1  no longer available
FabrikamAnalyzerRules         True 0.0.0.1  no longer available
ISESteroids                   True 2.3.0.46 2.3.0.64           
nScriptAnalyzerRules         False 0.1      0.1                
NtpTime                      False 1.1      1.1                
Pscx                         False 3.2.1.0  3.2.1.0            
PSScriptAnalyzer              True 1.1.0    1.4.0              
ScriptAnalyzer                True 0.5.1.0  no longer available
xDSCResourceDesigner          True 1.6.0.0  1.7.0.0 
 

Twitter This Tip! ReTweet this Tip!