Skip to content

Instantly share code, notes, and snippets.

@mhudasch
Last active April 12, 2025 17:09
Show Gist options
  • Save mhudasch/d14184d6e58f4aad398678242582fc18 to your computer and use it in GitHub Desktop.
Save mhudasch/d14184d6e58f4aad398678242582fc18 to your computer and use it in GitHub Desktop.
Real update of modules
Function Update-PSModule {
<#
.SYNOPSIS
Contrary to Update-Module command this command removes all previous versions of the module automatically.
.DESCRIPTION
The Update-PSModule command tries to find the current version of the given module. If there is a new version of the module
available the command removes all previous versions of the module and installs the new version. When the newest version of
the module is already installed the command does nothing unless the Force parameter is present. In case the Force parameter
is present the module will always be reinstalled with the newest available version.
.PARAMETER Name
The name of the module to update.
.PARAMETER Force
When this parameter is present all previous versions of the module are removed and the newest version of the module will be
installed even if the newest version was already installed.
.EXAMPLE
Update-PSModule Module1
Searches for the newest version of 'Module1' then removes all previous versions of it. After that installs the newest version.
If the newest version is already installed the command only removes old versions.
.EXAMPLE
Update-PSModule Module1
Searches for the newest version of 'Module1' then removes all previous versions of it. After that installs the newest version.
If the newest version is already installed the command removes this version too and reinstalls the newest version.
#>
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter(Mandatory=$true,Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias("n")]
[string]$Name,
[Parameter(Mandatory=$false,Position=1)]
[Alias("f")]
[Switch]$Force)
Process {
Write-Verbose "Looking for available module versions.";
$modules = Find-Module $Name -AllVersions | Sort-Object -Property Version;
$latest = $modules | Select-Object -Last 1;
Write-Verbose "Compare available module versions with the installed module versions.";
$installed = @(Get-InstalledModule -Name $Name -AllVersions);
if($installed.Count -eq 1 -and $installed[0].Version -eq $latest.Version) {
Write-Verbose "The latest version of the module is already installed.";
if(!$Force.IsPresent) {
return $installed[0];
}
}
$previous = [string]::Join(",`r`n", ($installed | ForEach-Object { ("{0}@{1}" -f $_.Name, $_.Version) }));
if($PSCmdlet.ShouldProcess($previous, "Removing all previous versions of the module.")) {
Write-Verbose "Removing all previous versions of the module."
$installed | Uninstall-Module | Out-Null;
Get-Module $Name -ListAvailable | Remove-Module -Force | Out-Null;
}
if($PSCmdlet.ShouldProcess(("{0}@{1}" -f $latest.Name, $latest.Version), "Installing module.")) {
Write-Verbose "Installing the latest version of the module";
$latest | Install-Module;
Import-Module $Name -Force;
return $(Get-InstalledModule -Name $Name -AllVersions | Select-Object -First 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment