Software in Chocolatey not synchronised after manually uninstall a software package
In chocolatey we’ve found problems when we manually uninstall software packages that are managed by chocolatey. In instance if you remove a certain software package in control panel in Windows, the deleted software is still visible in Chocolatey as installed software.
You will need to purchase a Chocolatey license in order to use the “Full Package Synchronization” feature which is only available in Chocolatey Business (C4B). After trying several things in PowerShell, we’ve managed to develop a script to automatically clean up the software packages in Chocolatey as soon it doesn’t exist anymore in Control Panel in Windows when a system administrator uninstalled it.
We’ve merged two known registry locations which are filtered by DisplayName. This variable is needed to compare it with the software packages in Chocolatey.
$registry_software = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*","HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | Where-Object DisplayName -NotLike "" | Select-Object DisplayName
In order to include the Chocolatey software titles in the script, create a variable with a list software commando from the Chocolatey module.
$chocolatey_packages = Get-ChocolateyPackage -LocalOnly
Optionally, create an array of software packages that you want to exclude from the comparison.
$chocolatey_packages_exclude = @("dotnet", "dotnet25")
Create a foreach loop that compares all software packages in Chocolatey with the software packages in registry. Include it with a regular expression to filter the Choclatey title strings in a useable format.
foreach ($package in $chocolatey_packages){
if ($package.name -notin $chocolatey_packages_exclude)
{
$get_package_title = choco list --localonly --name $package.Name --detailed | Where-Object {$_ -like "*Title:*"} | Out-String
$chocolatey_title = (([regex]::matches($get_package_title, '(?<=Title: ).+?(?=\|)')).Value).Trim()
$match = $registry_software | Where-Object DisplayName -EQ $chocolatey_title -ErrorAction SilentlyContinue
if($match)
{
Write-Host "$($package.name) is in sync with Windows"
}
else
{
Write-Host "Removing $($package.name)"
choco uninstall --name $package.name --force -y
}
}
else
{
Write-Host "$($package.name) is excluded from sync between local system and chocolatey installations"
}
}
If a Chocolatey software package is not in sync with the registry folders then the script will remove the software package from Chocolatey.
If it’s already in sync, the script will skip the software package.