Update Manually Installed PowerShell Modules from the PowerShell Gallery

There are PowerShell modules that ship with Windows 10 that weren't installed from the PowerShell Gallery using PowerShellGet so they can't be updated using the Update-Module cmdlet. This also applies for any modules that you've installed manually yourself.

The following PowerShell script retrieves a list of the most recent version of the modules in the all users path for PowerShell modules. It determines which ones weren't installed using PowerShellGet based on the hidden xml file that would exist in the module directory and then it determines if a newer version exists in one of the PowerShell Galleries that's registered on your computer:

 1Get-Module -ListAvailable |
 2Where-Object ModuleBase -like $env:ProgramFiles\WindowsPowerShell\Modules\* |
 3Sort-Object -Property Name, Version -Descending |
 4Get-Unique -PipelineVariable Module |
 5ForEach-Object {
 6    if (-not(Test-Path -Path "$($_.ModuleBase)\PSGetModuleInfo.xml")) {
 7        Find-Module -Name $_.Name -OutVariable Repo -ErrorAction SilentlyContinue |
 8        Compare-Object -ReferenceObject $_ -Property Name, Version |
 9        Where-Object SideIndicator -eq '=>' |
10        Select-Object -Property Name,
11                                Version,
12                                @{label='Repository';expression={$Repo.Repository}},
13                                @{label='InstalledVersion';expression={$Module.Version}}
14    }
15
16}

module-update1a.jpg

Installing the updated version is as simple as piping the previous results to ForEach-Object and nesting Install-Module with the Force parameter inside of it:

 1Get-Module -ListAvailable |
 2Where-Object ModuleBase -like $env:ProgramFiles\WindowsPowerShell\Modules\* |
 3Sort-Object -Property Name, Version -Descending |
 4Get-Unique -PipelineVariable Module |
 5ForEach-Object {
 6    if (-not(Test-Path -Path "$($_.ModuleBase)\PSGetModuleInfo.xml")) {
 7        Find-Module -Name $_.Name -OutVariable Repo -ErrorAction SilentlyContinue |
 8        Compare-Object -ReferenceObject $_ -Property Name, Version |
 9        Where-Object SideIndicator -eq '=>' |
10        Select-Object -Property Name,
11                                Version,
12                                @{label='Repository';expression={$Repo.Repository}},
13                                @{label='InstalledVersion';expression={$Module.Version}}
14    }
15
16} | ForEach-Object {Install-Module -Name $_.Name -Force}

module-update2a.jpg

Be sure to read the follow-up to this blog article PowerShell function to find information about module updates.

µ