Jump to content

How to search for multiple software name entry in registry?


Recommended Posts

I'm trying to search for either software name in the registry and if found then uninstall but I'm having issues with the logic.

 

#RequireAdmin

Uninstall()

Func Uninstall()
    $Logfile = @ScriptDir & "\app.log"
    $sSubkey = ""
    $sAppName = "App1"
    $sAppName2 = "App2"
    $sKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    $hFileOpen = FileOpen($Logfile, 9)
    For $a = 1 To 9999
        $sSubkey = RegEnumKey($sKey, $a)
        If @error Then
            FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " Apps not found, exiting")
            FileClose($hFileOpen)
            ExitLoop
        Else
            If StringInStr(RegRead($sKey & "\" & $sSubkey, "DisplayName"), $sAppName) Then
                $sUninst = RegRead($sKey & "\" & $sSubkey, "UninstallString")
                $new = StringRegExp($sUninst, '({.*?})', 1)
                For $i = 0 To UBound($new) - 1
                    FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & @CR & " Old string " & $sUninst & " New string " & "MsiExec.exe /X" & $new[$i] & " /qn UPWD:" & $CmdLine[1] & " RMCONFIG=1")
                    FileClose($hFileOpen)
                Next
            Else
                If StringInStr(RegRead($sKey & "\" & $sSubkey, "DisplayName"), $sAppName2) Then
                    $sUninst = RegRead($sKey & "\" & $sSubkey, "UninstallString")
                    $new = StringRegExp($sUninst, '({.*?})', 1)
                    For $i = 0 To UBound($new) - 1
                        FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & @CR & " Old string " & $sUninst & " New string " & "MsiExec.exe /X" & $new[$i] & " /qn UPWD:" & $CmdLine[1] & " RMCONFIG=1")
                        FileClose($hFileOpen)
                    Next
                EndIf
            EndIf
        EndIf
    Next
EndFunc   ;==>Uninstall

 

Link to comment
Share on other sites

18 minutes ago, antmar904 said:

having issues

Hmm... well, considering we don't want to uninstall everything on our computers (:o !), maybe you should describe what you mean a bit more 😐 

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

  • Moderators

@antmar904 instead of adding all of your apps inside the function, why not create an array of display names, and then just call your function against that? Below are two ways to do it: either passing a single display name each time if you want to do something else between each uninstall, or just passing the function the entire array. Written in Notepad on a machine with no AutoIt so you may need to modify to suit your needs :)

Pass single display name

Local $aApps = ["Adobe", "WinPCAP", "Putty"]

For $app in $aApps
    Uninstall($app)
Next


Func Uninstall($displayName)
    $Logfile = @ScriptDir & "\app.log"
    $sSubkey = ""
    $sKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    $hFileOpen = FileOpen($Logfile, 9)
    For $a = 1 To 9999
        $sSubkey = RegEnumKey($sKey, $a)
        If @error Then
            FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " App not found, exiting")
            FileClose($hFileOpen)
            ExitLoop
        Else
            If StringInStr(RegRead($sKey & "\" & $sSubkey, "DisplayName"), $displayName) Then
                $sUninst = RegRead($sKey & "\" & $sSubkey, "UninstallString")
                $new = StringRegExp($sUninst, '({.*?})', 1)
                For $i = 0 To UBound($new) - 1
                    FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & @CR & " Old string " & $sUninst & " New string " & "MsiExec.exe /X" & $new[$i] & " /qn UPWD:" & $CmdLine[1] & " RMCONFIG=1")
                    FileClose($hFileOpen)
                Next
            EndIf
        EndIf
    Next
EndFunc   ;==>Uninstall

 

Pass entire array

Local $aApps = ["Adobe", "WinPCAP", "Putty"]

Uninstall($aApps)

Func Uninstall($aNames)
    $Logfile = @ScriptDir & "\app.log"
    $sSubkey = ""
    $sKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    $hFileOpen = FileOpen($Logfile, 9)
    
    For $displayName In $aNames
        For $a = 1 To 9999
            $sSubkey = RegEnumKey($sKey, $a)
            If @error Then
                FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " App not found, exiting")
                FileClose($hFileOpen)
                ExitLoop
            Else
                If StringInStr(RegRead($sKey & "\" & $sSubkey, "DisplayName"), $displayName) Then
                    $sUninst = RegRead($sKey & "\" & $sSubkey, "UninstallString")
                    $new = StringRegExp($sUninst, '({.*?})', 1)
                    For $i = 0 To UBound($new) - 1
                        FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & @CR & " Old string " & $sUninst & " New string " & "MsiExec.exe /X" & $new[$i] & " /qn UPWD:" & $CmdLine[1] & " RMCONFIG=1")
                        FileClose($hFileOpen)
                    Next
                EndIf
            EndIf
        Next
    Next
EndFunc   ;==>Uninstall

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

@JLogan3o13 Thank you however this is not finding that apps when trying to use the displayname (first option you posted).

I believe what is happening  when running my script it is being forced to read from the WOW6432Node instead of the key I specified in the script.

Edited by antmar904
Link to comment
Share on other sites

  • Moderators

Yes, you need to account for both on a 64 bit system. If you search the forum, you will find a number of examples for hitting both hives in the registry.

Edit: or you can wait for someone to do it for you lol

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

14 minutes ago, JLogan3o13 said:

If you search the forum, you will find a number of examples for hitting both hives in the registry.

Yes :).

Here e.g. a script created by @Subz (Link : registry-search )

#include <Array.au3>

Local $iCount = 1 ;~ Count number of entries found
Local $sSeparator = "," ;~ Change to semi-colon if you wish
Local $sFileName = @ScriptDir & "\Uninstalls.txt"
Local $hFileOpen = FileOpen($sFileName, 10)

;~ Gets All 32-Bit Uninstall Keys
_RegReadUninstall("HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\")
;~ Gets All 64-Bit Uninstall Keys
_RegReadUninstall("HKLM64\Software\Microsoft\Windows\CurrentVersion\Uninstall\")

FileClose($hFileOpen)
ShellExecute($sFileName) ;~ Open the File afterwards

Func _RegReadUninstall($_sRegKeyHive = "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\")
    $_sRegKeyHive = StringRight($_sRegKeyHive, 1) = "\" ? $_sRegKeyHive : $_sRegKeyHive & "\" ;~ Makes sure last entry is backslash
    Local $a_RegReadUninstall[1][6] = [["Count", "Registry Key", "DisplayName", "UninstallString", "QuietUninstallString", "(Standard"]]
    Local $i = 1
    While 1
        $sRegKeyName = RegEnumKey($_sRegKeyHive, $i)
            If @error then ExitLoop
        $sRegResults =  _
        RegRead($_sRegKeyHive & $sRegKeyName,"DisplayName") & "|" & _
        RegRead($_sRegKeyHive & $sRegKeyName, "UninstallString") & "|" & _
        RegRead($_sRegKeyHive & $sRegKeyName, "QuietUninstallString") & "|" & _
        RegRead($_sRegKeyHive & $sRegKeyName, "(Standard)")
        If StringStripWS(StringReplace($sRegResults, "|", ""), 8) = "" Then
            $i += 1
            ContinueLoop
        EndIf
        _ArrayAdd($a_RegReadUninstall, $iCount & "|" & $_sRegKeyHive & $sRegKeyName & "|" & $sRegResults)
        $i += 1
        $iCount += 1
    WEnd
    FileWrite($hFileOpen, _ArrayToString($a_RegReadUninstall, $sSeparator) & @CRLF)
EndFunc

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

ok, i'm trying to read both hives but this is not working as expected.

#RequireAdmin
#include <StringConstants.au3>

Global $aApps = ["App1", "App2"], $Logfile = @ScriptDir & "\app.log", $hFileOpen = FileOpen($Logfile, 9)

For $app In $aApps
    Uninstallx86($app)
Next

For $app In $aApps
    Uninstallx64($app)
Next

Exit

Func Uninstallx86($displayName)
    $sKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    $sSubkey = ""
    For $a = 1 To 9999
        $sSubkey = RegEnumKey($sKey, $a)
        FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & $sKey & "\" & $sSubkey)

        If @error Then
            FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " x86 App not found.")
            FileClose($hFileOpen)
            ExitLoop
        Else
            If StringInStr(RegRead($sKey & "\" & $sSubkey, "DisplayName"), $displayName) Then
                $sUninst = RegRead($sKey & "\" & $sSubkey, "UninstallString")
                $new = StringRegExp($sUninst, '({.*?})', 1)
                For $i = 0 To UBound($new) - 1
                    FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & @CR & " Old string " & $sUninst & " New string " & "MsiExec.exe /X" & $new[$i] & " /qn UPWD:" & $CmdLine[1] & " RMCONFIG=1")
                Next
            EndIf
        EndIf
    Next
EndFunc   ;==>Uninstallx86

Func Uninstallx64($displayName)
    $sKey = "HKLM64\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    $sSubkey = ""
    For $a = 1 To 9999
        $sSubkey = RegEnumKey($sKey, $a)
        FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & $sKey & "\" & $sSubkey)
        ;MsgBox(0, "", $sKey & "\" & $sSubkey)
        If @error Then
            FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " x64 App not found, exiting")
            FileClose($hFileOpen)
            Exit
        Else
            If StringInStr(RegRead($sKey & "\" & $sSubkey, "DisplayName"), $displayName) Then
                $sUninst = RegRead($sKey & "\" & $sSubkey, "UninstallString")
                $new = StringRegExp($sUninst, '({.*?})', 1)
                For $i = 0 To UBound($new) - 1
                    FileWriteLine($Logfile, @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & @CR & " Old string " & $sUninst & " New string " & "MsiExec.exe /X" & $new[$i] & " /qn UPWD:" & $CmdLine[1] & " RMCONFIG=1")
                    FileClose($hFileOpen)
                Next
            EndIf
        EndIf
    Next
EndFunc   ;==>Uninstallx64

 

Link to comment
Share on other sites

Assuming that the app or apps that you want to uninstall were installed by Windows Installer, I would use the Uninstall method of the Win32_Product WMI class to uninstall them.  All that would be required would be to do a query by name to find the particular instance of the application, and if successful, use the Uninstall method of that instance.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...