Getting Latest PowerShell Gallery Module Version

by Nov 7, 2016

On www.powershellgallery.com, Microsoft hosts a public script and module repository where you can exchange PowerShell code with others (see more on their site).

To use the repository, you either need PowerShell 5 or install the PowerShellGet module manually (which is available for download on powershellgallery.com). You then have cmdlets like Find/Save/Install/Update/Remove-Script/Module.

What’s missing is a quick way of finding out what the most current version of a published module is. Here is a solution:

function Get-PublishedModuleVersion($Name)
{
   # access the main module page, and add a random number to trick proxies
   $url = "https://www.powershellgallery.com/packages/$Name/?dummy=$(Get-Random)"
   $request = [System.Net.WebRequest]::Create($url)
   # do not allow to redirect. The result is a "MovedPermanently"
   $request.AllowAutoRedirect=$false
   try
   {
     # send the request
     $response = $request.GetResponse()
     # get back the URL of the true destination page, and split off the version
     $response.GetResponseHeader("Location").Split("/")[-1] -as [Version]
     # make sure to clean up
     $response.Close()
     $response.Dispose()
   }
   catch
   {
     Write-Warning $_.Exception.Message
   }
}

Get-PublishedModuleVersion -Name ISESteroids

When you run Get-PublishedModuleVersion and submit the name of a published module, the result looks like this:

 
PS C:\> Get-PublishedModuleVersion -Name ISESteroids

Major  Minor  Build  Revision
-----  -----  -----  --------
2      6      3      25      
 

This approach is very, very fast and can be used to check against the versions of installed modules to see whether you are up-to-date.

Twitter This Tip! ReTweet this Tip!