Find an uninstall command in registry

It's sometimes useful to be able to find an uninstall command that you can run, from the registry, for a named application.

POWERSHELL SCRIPT

This script allows you to enter an application's name. The script will then search the x64 and x32 registry keys to find, if possible, the uninstall command for you to run to remove the application, and where possible, if it's an EXE (non-MSI) installer, a guess of the command line.

THE SCRIPT

# Prompt for application name
$appName = Read-Host "Enter the name of the application you want to find the uninstall command for"

# Define registry paths to search for uninstall commands
$regPaths = @(
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
    "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
)

# Initialize variable to track if the application is found
$appFound = $false

# Search registry paths for the application
foreach ($path in $regPaths) {
    Get-ChildItem -Path $path -ErrorAction SilentlyContinue | ForEach-Object {
        $displayName = (Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue).DisplayName
        $uninstallString = (Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue).UninstallString

        # Check if the display name matches the application name
        if ($displayName -and $displayName -like "*$appName*") {
            Write-Output "`nFound Application: $displayName"

            # Check if it's an MSI uninstall command (GUID-based)
            if ($uninstallString -match 'MsiExec\.exe /X\{[A-F0-9\-]+\}') {
                # Convert to a reliable silent uninstall command for MSI
                $silentCommand = "$uninstallString /qn /norestart"
                Write-Output "Corrected Silent Uninstall Command: $silentCommand"
                Write-Output "If the application was not installed via MSI the command may not actually work"
            } elseif ($uninstallString) {
                # For non-MSI applications, we show the uninstall string as is
                Write-Output "Uninstall Command (non-MSI): $uninstallString"
                # Guess silent parameters for common EXE-based uninstallers
                if ($uninstallString -match '\.exe') {
                    $silentCommand = "$uninstallString /silent /quiet /norestart"
                    Write-Output "Suggested Silent Uninstall Command (non-MSI): $silentCommand"
                    
                }
            } else {
                Write-Output "No uninstall command found for '$displayName'."
            }

            $appFound = $true
        }
    }
}

# Check if application was found
if (-not $appFound) {
    Write-Output "No application found matching '$appName'."
}

# Optional: Uncomment the next lines to automatically execute the command (use with caution!)
# if ($silentCommand) {
#     Start-Process "cmd.exe" -ArgumentList "/c $silentCommand" -Wait
#     Write-Output "Uninstallation initiated."
# }