Skip to content

Module Cleanup

John G Hohengarten edited this page Apr 25, 2019 · 16 revisions

Author:

@wsuhoey (John G Hohengarten) aka "shoe"

Contributors:

For the logic of finding other powershell instances:

@kilasuit (@ryanyates1990)

@ck for the foreach logic for skipping task if no processes return

@wsmelton for the powershell* part

ben.thul (Ben Thul) for multiple old version logic via [array] and foreach. Thanks!

Purpose:

Script for cleaning up all older versions of the module and updating your machine to the latest release.

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

Caveats/Known Issues:

  • Does NOT consider or check if dbachecks module is installed, which might have a specific dependency on a required or minimum version of dbatools to be installed.

Script:

$module = "dbatools"

[array]$installedversion = Get-Module $module -ListAvailable | Select-Object Version
Write-Host "The currently installed version(s) of $module is" $installedversion.Version

#Find-Module $module # check what latest version is in PSGallery, full results
#Find-Module $module | Select-Object Version, PublishedDate # check what latest version is in PSGallery

$newestversion = Find-Module $module | Select-Object Version
Write-Host "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-Host "You have the latest version of $module installed."
	Write-Host "However, you ALSO have OLD versions $($olderversions.Version) installed, too."

	Write-Host "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-Host "(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-Host "Uninstalling old version $module $($oldversion.Version)"
		Uninstall-Module $module -RequiredVersion $($oldversion.Version)
	}
	Write-Host "Cleanup of older $module versions completed!"
	Write-Host "Recommended to exit this powershell.exe or powershell_ise.exe"
}

	elseif ( $installedversion.Version -lt $newestversion.Version -and !($installedversion.Version -eq $newestversion.Version) )
	{
		Write-Host "New version" $newestversion.Version "of $module detected..."

		Write-Host "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-Host "(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-Host "Now updating $module to" $newestversion.Version "..."
		Get-Module $module | Remove-Module
		Update-Module $module

		foreach ($oldversion in $installedversion )
		{
			Write-Host "Uninstalling old version $module $($oldversion.Version)"
			Uninstall-Module $module -RequiredVersion $($oldversion.Version)
		}
		Write-Host "Update completed!"
		Write-Host "Recommended to exit this powershell.exe or powershell_ise.exe"
	}

		ELSE
		{
			Write-Host "No update needed."
		}

Get-Module dbatools -ListAvailable