Skip to content

Module Cleanup

John G Hohengarten edited this page Feb 5, 2020 · 16 revisions

Update

Starting with dbatools version 1.0.62, we now include a revised version of this script with the module.

You can find it under the base module path:

(Get-InstalledModule dbatools).InstalledLocation

or by running the below one-liner in a new session:

& "$((Get-InstalledModule dbatools).InstalledLocation)/cleanup.ps1"

Purpose

We release new versions of the module frequently. The code below can be utilized for cleaning up the older versions of our module, and ensure you are on the latest release.

Note that you can use this script for ANY module in the PSGallery, by just adjusting the $module variable.

Notice:

  • This script is provided as-is and as always you should review/understand what a script does prior to running it in your environment.

In addition, this script does not account for any module that may have a requirement on around a specific version of dbatools.

Script:

[cmdletbinding()]
param(
    $module = "dbatools"
)

[array]$installedVersion = Get-InstalledModule $module | Select-Object Version
Write-Output "The currently installed version(s) of $module is: " ($installedVersion.Version -join ",")

$newestVersion = Find-Module $module | Select-Object Version
Write-Output "The latest version of $module in the PSGallery is" $newestVersion.Version

if ( $installedVersion.Version -lt $newestVersion.Version -and $installedVersion.Version -eq $newestVersion.Version )
{
    $olderVersions = $installedVersion | Where-Object Version -lt $newestVersion.Version
    Write-Output "You have the latest version of $module installed."
    Write-Output "However, you ALSO have OLD versions $($olderVersions.Version) installed, too."

    Write-Output "WARNING: Finding and killing all other instances of powershell.exe and powershell_ise.exe to prevent uninstall issues later due to being in-use."
    Write-Output "(This could impact Agent Jobs if run on a server that has PowerShell running)."
    Get-Process powershell* | Where-Object Id -NE $PID | ForEach-Object { Stop-Process -Confirm $_ }

    Get-Module $module | Remove-Module

    Update-Module $module

    foreach ( $oldVersion in $olderVersions ) {
        Write-Output "Uninstalling old version $module $($oldVersion.Version)"
        Uninstall-Module $module -RequiredVersion $($oldVersion.Version)
    }
    Write-Output "Cleanup of older $module versions completed!"
    Write-Output "Recommended to exit this powershell.exe or powershell_ise.exe"
} elseif ( $installedVersion.Version -lt $newestVersion.Version -and !($installedVersion.Version -eq $newestVersion.Version) ) {
    Write-Output "New version" $newestVersion.Version "of $module detected..."
    Write-Output "WARNING: Finding and killing all other instances of powershell.exe and powershell_ise.exe to prevent uninstall issues later due to being in-use."
    Write-Output "(This could impact Agent Jobs if run on a server that has PowerShell running)."
    Get-Process powershell* | Where-Object Id -NE $PID | ForEach-Object { Stop-Process -Confirm $_ }

    Write-Output "Now updating $module to" $newestVersion.Version "..."
    Get-Module $module | Remove-Module
    Update-Module $module

    foreach ($oldVersion in $installedVersion ) {
        Write-Output "Uninstalling old version $module $($oldVersion.Version)"
        Uninstall-Module $module -RequiredVersion $($oldVersion.Version)
    }
    Write-Output "Update completed!"
    Write-Output "Recommended to exit this powershell.exe or powershell_ise.exe"
} else {
    Write-Output "No update needed."
}

Get-InstalledModule dbatools