Dynamic Uninstall

The Scenario: App installs but may update itself or generate a random key every time (idiots).
The Solve: If the below script comes out correctly formatted, it has the logic to detect the uninstall strings and piece together the uninstall. Keep in mind the app is 32-bit and I didn’t want to spend more time than I had to.

#Find dynamic uninstall string
$DName = 'DisplayNameHere'
$Exe= 'ExpectedUninstallerName'
$QUS = 'KeyDataName'
$root = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
$root64 = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
if ($is64bit) {
    $keys = gci $root64
}
else {
    $keys = gci $root
}
foreach ($key in $keys) {
    $Inst = (get-itemproperty $key.pspath | ? {$_.DisplayName -match $DName}).$QUS
    $Split1 = $Inst -split '/' | ? {$_.trim() -ne '' }
    $Split2 = $Split1 -split '`r`n'
    foreach ($Split in $Split2) {
        if ($Split -like "*$Exe*") {
            $Uninst = $Split.Replace("`'",'')
        }
        else {
            $Parameters += '/' + $Split
        }
    }
}

#Execute gathered string
Execute-Process -Path $Uninst -Parameters $Parameters

Matt,

I’ve seen other people on here post similar scripts to uninstall software. Out of curiosity, why not use the get-installedapplication function to get the uninstall string? That way you let the function figure out if it’s 64-bit or not…

Something like this:

$VC2013 = ((Get-InstalledApplication -Name ‘Microsoft Visual C++ 2013 Redistributable (x64) - 12.0.30501’).UninstallString -split ‘" ‘).Trim(’"’)
If ($VC2013) {
$VC2013Uninst = $VC2013[0]
$VC2013Params = $VC2013[1]
Execute-Process -Path $VC2013Uninst -Parameters “$VC2013Params /quiet /norestart”
}

Or my post here, “http://psappdeploytoolkit.com/forums/topic/execute-process-odd-behavior/#post-2248”.

Simple, that function only gets certain values:
UninstallSubkey
ProductCode
DisplayName
DisplayVersion
UninstallString
InstallSource
InstallLocation
InstallDate
Publisher
Is64BitApplication

The function would be best served by allowing input to include (partial) display name, specific reg value (quietuninstallstring here). This method also generated the Uninstall command for me so that I didn’t need to know switches. I put this into a function which I used in both preinstall and uninstall state. Also expanded it a little more to include logging…it’s much more effective now.

The get-installedapplication does support partial display names. It supports name, exact and even wildcard. You could submit a feature request to have QuietUninstall added to the list of values it finds or even just add it yourself- QuietUninstallString = $regKeyApp.QuietUninstallString. I think I’m going to do that. Thanks for the idea. My way above got the default uninstall parameters, I just manually added the /quiet /norestart since I wasn’t pulling the QuietUninstall key and didn’t have all the parameters. But it did have the /uninstall. So it looks like both of our methods get the job done, just in different ways.