How-To Auto-Update PowerShell modules

Here’s a little snippet i extensively use in my scripts. It helps to check if there’s updates for your PowerShell modules and installs them if available. In this example, I want to use two different PowerShell modules bccontainerhelper and sqlserver.

Actually, this could be used in all PowerShell scripts that don’t depend on a specific version. Since there is known to be a lot of movement in the development of the bccontainerhelper, it is usually worth using it for that alone.

The function

The function itself tries to read the latest version from the PSGallery and checks it against the installed version. In case there’s a newer version online, the script automatically installs it. Otherwise, the current version is used.

# Auto-Update PowerShell
function CheckAndUpdateModule {
    param (
        [string]$Module = '',
        [bool]$UninstallFirst = $false
    )

    # Read the currently installed version
    $installed = Get-Module -ListAvailable -Name $Module
    
    # Install if not existing
    if(!$installed) {
        Install-Module -Name $Module -Force -AllowClobber -Verbose:$false
        exit
    }

    # There might be multiple versions
    if ($installed -is [array]) {
        $installedversion = $installed[0].version
    }
    else {
        $installedversion = $installed.version
    }
    
    # Lookup the latest version online
    $online = Find-Module -Name $Module -Repository PSGallery -ErrorAction Stop
    $onlineversion = $online.version  

    # Compare the versions
    if ($onlineversion -gt $installedversion) {
        
        # Uninstall the old version
        if ($UninstallFirst -eq $true) {
            Write-Host "Uninstalling prior Module $Module version $installedversion"
            Uninstall-Module -Name $Module -Force -Verbose:$false
        }
        
        Write-Host "Installing newer Module $Module version $onlineversion"
        Install-Module -Name $Module -Force -AllowClobber -Verbose:$false
    } else {
        Write-Host "Module $Module version $installedversion loaded"
    }
}

How to Auto-Update PowerShell modules

Now you could use the function CheckAndUpdateModule() to load any number of modules defined in the array. Using the -UninstallFirst parameter lets the script uninstall the outdated version first.

# Update of a single module
CheckAndUpdateModule -Module "bccontainerhelper" -UninstallFirst $true

# Or multiple modules at once
[string[]]$ModuleArray = "bccontainerhelper"
          $ModuleArray += "SqlServer"
          
foreach ($Module in $ModuleArray) {
    Write-Host "Checking module $Module"
    CheckAndUpdateModule -Module $Module -UninstallFirst $true
}

... is a technical consultant and developer at Comsol Unternehmenslösungen AG in Kronberg/Taunus. Major tasks are the architecture and implementation of complex, usually cross-system applications in and around Microsoft Dynamics 365 Business Central.

2 comments

  1. Cool idea. Does this also uninstall the old version of the module before installing the new one? If not, you might end up having installed multiple versions of bccontainerhelper. At least that is what I have experienced in the past.

    Reply

    1. Hi Waldemar, yes, absolutely, we are struggling with the multiple version problem as well! Especially in our Build pipelines.

      That’s why I set the -AllowClobber parameter. This lets the last installed module version “win” in case of command-conflicts.

      Thank you for your input, I’ve extended the script with the possibility to uninstall the prior version before installation. Seems to work as expected.

      Reply

Leave a Reply

Your email address will not be published.