How do I get a product code from the Windows Installer database with a just a product name?

I am trying to uninstall a service with msiexec.  The /X argument takes a product code.  Sense I will not have the original msi I want to use the product code to remove the service.  Is there a way to query the Installer service for the proper product code (from the command line) that I can then use to remove the service?
cbitter78Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vadim RappCommented:
You would have to write a script that would take the name of the product as input and give you the product code, or actually run msiexec /x that code.

The script can either go after hklm\software\windows\currentversion\uninstall, or use WMI and run query like
select IdentifyingNumber from Win32_Product where name='My Product'
cbitter78Author Commented:
I was leaning towards this solution.  Know of any examples?   I prefer C#
Vadim RappCommented:
WMI Code Creator and Scriptomatic will create WMI script automatically, you choose the language.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Vadim RappCommented:
I'd like to note that even though WMI solution appears more robust, on the several machines where I tried it, including my own, it succeeded on only one out of 4, and failed to retrieve installed products on others. I'm sure it would be possible to find out why and try to repair it (probably WMI database), but hklm\software\windows\currentversion\uninstall way would worked right away everywhere.
cbitter78Author Commented:
I liked the WMI scripting utility's.   However in the end I wrote a C# app that looks in the registry and iterates through each installed product until it finds the right product key.


The WMICodeCreator generates C# but I did not use this code.   I also found that WMI is not the best choice so I went with direct registry access.
cbitter78Author Commented:
Ok so looking in the Registry is NOT the way to go.  you will find some products this way but its hit or miss.   Here is a C# method that does it the right way.  
        /// <summary>
        /// This method REQUIRES a referance to msi.dll in Windows\System32.  (com interopt)
        /// </summary>
        private static void ListMsiInfo()
        {
            // List of all the attributes you can query for.
            var attributes = new string[]
                                 {
                                     "HelpLink",
                                     "HelpTelephone",
                                     "InstallDate",
                                     "InstallLocation",
                                     "InstalledProductName",
                                     "InstallSource",
                                     "LocalPackage",
                                     "ProductID",
                                     "Publisher",
                                     "PackageName",
                                     "RegCompany",
                                     "RegOwner",
                                     "URLInfoAbout",
                                     "URLUpdateInfo",
                                     "VersionMinor",
                                     "VersionMajor",
                                     "VersionString",
                                     "Transforms",
                                     "Language",
                                     "AssignmentType",
                                     "PackageCode",
                                     "PackageName",
                                     "ProductIcon",
                                     "ProductName",
                                     "Version",
                                     "InstanceType",
                                 };
 
            // Get a referance to the WindowsInstaller.Installer via com interopt.
            WindowsInstaller.Installer winInstaller = null;
            Type oType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
            if (oType == null) return;
            winInstaller = (WindowsInstaller.Installer) Activator.CreateInstance(oType);
            if (winInstaller == null) return;
            
            // Iterate through all the productIds of the installed apps.
            foreach (string productId in winInstaller.Products)
            {
                // Write out the product code.
                Console.WriteLine(productId);
 
                // try to get and print each attribute
                foreach (string attribute in attributes)
                {
                    string aVal = winInstaller.get_ProductInfo(productId, attribute);
                    if (string.IsNullOrEmpty(aVal)) continue;
                    Console.WriteLine("{0}:{1}", attribute, aVal);
                }
            }
        }

Open in new window

cbitter78Author Commented:
Even with the above code you cant get the product Code..   I wish I could reopen this question.
Vadim RappCommented:
Sorry, post-closing assistance is contingent on the grade A. Feel free to post new one.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Installation

From novice to tech pro — start learning today.