Using PowerShellForGitHub to Check for CLI Tool Updates

SimonSimon
3 min read

Updated with a modular example.

If you use CLI tools on both Windows and Linux, you may have noticed that not all of them notify users about updates or include auto-update mechanisms. This guide demonstrates how to use the PowerShellForGitHub module to check for new software releases.

Overview

The PowerShellForGitHub module enables interaction with GitHub directly from the PowerShell command line. It provides cmdlets for managing GitHub repositories, issues, pull requests, and other resources. This module simplifies automating tasks, retrieving information, and integrating GitHub operations into scripts and workflows.

Installation

To install the PowerShellForGitHub module, run the following command:

Install-Module -Name PowerShellForGitHub

Set Up Your GitHub Access Token

Before using the module, configure your GitHub Access Token and then:

Get-Help Set-GitHubConfiguration -ShowWindow

Usage

A tool I use often is Microsoft Graph API CLI, let's get the available releases.

Get-GitHubRelease -Uri 'https://github.com/microsoftgraph/msgraph-cli'

Only get the latest release.

Get-GitHubRelease -Uri 'https://github.com/microsoftgraph/msgraph-cli' -Latest

Save the latest release to a variable.

$release =  Get-GitHubRelease -Uri 'https://github.com/microsoftgraph/msgraph-cli' -Latest

Get the tag name which in this case for Microsoft Graph API is also the version.

$release.tag_name

Practical Example: PowerShellForGitHub

Using PowerShellForGithub and PowerShell we can compare the installed version of a tool and latest release to determine if a newer version is available for install.

For example, the Microsoft Graph CLI does not have an auto-update mechanism but you can run the mgc --version command to get the currently installed version (e.g., 1.8.0) and Get-GitHubRelease -Uri 'https://github.com/microsoftgraph/msgraph-cli' -Latest to get the latest version (e.g., 1.9.0 at the time of writing).

Getting the currently installed version of Microsoft Graph CLI

The currently installed version of Microsoft Graph CLI

The latest available version of Microsoft Graph CLI

  1. From Windows Terminal, open the PowerShell profile with code $PROFILE (requires VSCode) or vim $PROFILE (requires VIM)

  2. Add the following Function to match the numerical parts of a software version number.

     function Get-NumericalVersion
     {
         param (
             [string]$versionString
         )
    
         # Use regex to match the numerical parts of a software version number
         if ($versionString -match '(\d+\.\d+\.\d+)')
         {
             return $matches[0]
         } else
         {
             return $null
         }
     }
    
  3. This is optional but add the following command below the function to suppress telemetry

     Set-GitHubConfiguration -SuppressTelemetryReminder
    
  4. Add the following list to track the tool, Github Release URL and terminal command to get the locally installed version:

     # Define a list of CLI tool GitHub URLs and their corresponding installed version commands
     $cliTools = @(
         @{ Name = "Microsoft Graph CLI"; Url = "https://github.com/microsoftgraph/msgraph-cli"; Command = "mgc --version" }
         # Add more CLI tools here
         # @{ Name = "Another CLI Tool"; Url = "https://github.com/another/tool"; Command = "another-tool --version" }
     )
    
  5. Create a For Loop to iterate through the list and compare the latest release to the local version.

     # Loop through each CLI tool and check for updates
     foreach ($tool in $cliTools) {
         # Get the latest release from GitHub
         $latestRelease = Get-GitHubRelease -Uri "$($tool.Url)" -Latest
    
         # Get the installed version of the CLI tool
         $installedVersion = Invoke-Expression $tool.Command
    
         # Extract the numerical part of the version strings
         $latestVersion = Get-NumericalVersion -versionString $latestRelease.tag_name
         $installedVersion = Get-NumericalVersion -versionString $installedVersion
    
         # Notify if there is a newer version available
         if ($latestVersion -gt $installedVersion) {
             Write-Host "`n[+] New $($tool.Name) Release available: $latestVersion (Installed: $installedVersion)`n" -ForegroundColor Green
         } else {
             Write-Host "`n[-] $($tool.Name) is up to date: $installedVersion`n" -ForegroundColor Yellow
         }
     }
    

Next time you launch PowerShell, it will check to see if there is a new version of the Microsoft Graph CLI.

Notification that a new version of Microsoft Graph API CLI is available!

And there it is, a pretty simple example of how to use PowerShellForGithub to monitor CLI tools for the latest release.

0
Subscribe to my newsletter

Read articles from Simon directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Simon
Simon