windowsbatch-fileregistrybatch-processingregistrykey

How to find registry folder by match in it`s value


I am writing a batch file to search the registry. I need to find the folder inside HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products which ProductName key equals to "MyProduct". I need to find this folder and delete it.

This folder I want to delete will look like this: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\1A0614C849C672CF0A680DCFA3921735


Solution

  • In this example, change MyProduct to your actual ProductName string leaving the closing doublequote untouched, on line 4:

    @Echo Off
    SetLocal EnableExtensions
    
    Set "App=MyProduct"
    Set "Key=HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products"
    
    For /F "Delims=" %%G In ('^""%SystemRoot%\System32\reg.exe" Query "%Key%" /S /F
     "%App%" /D /E 2^>NUL ^| "%SystemRoot%\System32\findstr.exe" /I /R /X
     "%Key:\=\\%\\[^^^\\]*"^"'
    ) Do Echo="%SystemRoot%\System32\reg.exe" Delete "%%G" /F ^>NUL
    Pause
    

    The above will only print the deletion line that you intend to run. Once you are satisfied it is correct, to actually delete it, change the code to the following, (remembering to change your ProductName string again). Please note, that as you're deleting a key from HKEY_LOCAL_MACHINE, you will most likely need to run this script elevated, or as a user having sufficient permissions for doing so.:

    @Echo Off
    SetLocal EnableExtensions
    
    Set "App=MyProduct"
    Set "Key=HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products"
    
    For /F "Delims=" %%G In ('^""%SystemRoot%\System32\reg.exe" Query "%Key%" /S /F
     "%App%" /D /E 2^>NUL ^| "%SystemRoot%\System32\findstr.exe" /I /R /X
     "%Key:\=\\%\\[^^^\\]*"^"'
    ) Do "%SystemRoot%\System32\reg.exe" Delete "%%G" /F >NUL