title: Wix: Adding registry keys on Uninstall
body: <p>I'm given a requirement in which our installer has to restore/write a registry key upon uninstall/removal of the installer. To achieve this, I'm calling a .cmd file upon uninstall via Custom Action. Here is its code:</p>
  
  <pre><code>set SSKey=HKLM\Software\MyProduct\Installed Modules\TestModule
  reg.exe add "%SSKey%" /v AppName  /d TestModule /f /reg:32
  reg.exe add "%SSKey%" /v Type /t REG_DWORD /d 7 /f /reg:32
  reg.exe add "%SSKey%" /v AppName  /d TestModule /f /reg:64
  reg.exe add "%SSKey%" /v Type /t REG_DWORD /d 7 /f /reg:64
  </code></pre>
  
  <p>Here is the Custom Action snippet calling this .cmd file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"&gt;
    &lt;Product Id="*" Name="RegOnUninstall" Language="1033" Version="1.0.0.0" Manufacturer="Testing" UpgradeCode="3dbec931-5475-40b3-a002-824c09aad36c"&gt;
      &lt;Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" InstallPrivileges="elevated" /&gt;
  
      &lt;MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." /&gt;
      &lt;MediaTemplate /&gt;
  
      &lt;Feature Id="ProductFeature" Title="RegOnUninstall" Level="1"&gt;
        &lt;ComponentRef Id="RSS" /&gt;
      &lt;/Feature&gt;
      &lt;Property Id="WixQuietExecCmdLine" Value="&amp;quot;[#RSS.cmd]&amp;quot; -RSS"/&gt;
      &lt;CustomAction Id="SS" BinaryKey="WixCA" DllEntry="WixQuietExec" Execute="deferred" Return="check"/&gt;
      &lt;InstallExecuteSequence&gt;
        &lt;Custom Action="SS" Before="RemoveFiles"&gt;Installed AND REMOVE&lt;/Custom&gt;
      &lt;/InstallExecuteSequence&gt;
   &lt;/Product&gt;
  
    &lt;Fragment&gt;
      &lt;Directory Id="TARGETDIR" Name="SourceDir"&gt;
        &lt;Directory Id="ProgramFilesFolder"&gt;
          &lt;Directory Id="INSTALLFOLDER" Name="RegOnUninstall" &gt;
            &lt;Component Id="RSS" Guid="*" &gt;
              &lt;File Id="RSS.cmd" KeyPath="yes" Source="D:\Scripts\RestSS.cmd"/&gt;
            &lt;/Component&gt;
          &lt;/Directory&gt;
        &lt;/Directory&gt;
      &lt;/Directory&gt;
    &lt;/Fragment&gt;
  </code></pre>
  
  <p><strong>Note:</strong> This custom action is in WixUtilExtension.</p>
  
  <p>Although installer is using Elevated Privileges, this script is not able to write in HKLM. Because of this failure, the installer is unable to Uninstall from the machine.</p>
  
  <p>If I run this .cmd script in a Command Prompt with Admin privileges, it would work fine.</p>
  
  <p>I tried to write Managed Custom Action and it was facing similar issue i.e. can't write to HKLM.</p>
  
  <p><strong>My main purpose is to write registry key upon Uninstall.</strong> </p>
  
  <p>Any help would be really appreciated....
  Thanks</p>