// Get information about MSI-installed products that match a specified product name regex. // If no products match, or no regex is specified, set errorlevel 1. // // An older version of this script would list all MSI-installed products if no regex was // supplied. Given that the motivation for this script was to help with software deletion, // this proved dangerous when the match regex was a computed value. To match all products // with this version, use an explicit . regex. // // Stephen Thomas // Last updated 19-Jun-2015 // // This is free software. Do whatever you like with it except // hold me accountable for any grief it causes you. // Getting at the registry from jscript requires bizarre contortions: see // http://msdn.microsoft.com/en-us/library/aa392722 // http://msdn.microsoft.com/en-us/library/aa389294 // http://msdn.microsoft.com/en-us/library/aa392730 // http://msdn.microsoft.com/en-us/library/aa394616 reg = (function() { var registry = GetObject('winmgmts:{impersonationLevel=impersonate}!root/default:StdRegProv'); return { HKCR: 0x80000000, HKCU: 0x80000001, HKLM: 0x80000002, HKU: 0x80000003, HKCC: 0x80000005, enumKey: function (hive, key) { var method = registry.Methods_.Item('EnumKey'); var inParam = method.InParameters.SpawnInstance_(); inParam.hDefKey = hive; inParam.sSubKeyName = key; var outParam = registry.ExecMethod_(method.Name, inParam); if (outParam.ReturnValue == 0) { return outParam.sNames.toArray(); } else return []; }, getStringValue: function (hive, key, name) { var method = registry.Methods_.Item('GetStringValue'); var inParam = method.InParameters.SpawnInstance_(); inParam.hDefKey = hive; inParam.sSubKeyName = key; inParam.sValueName = name; var outParam = registry.ExecMethod_(method.Name, inParam); if (outParam.ReturnValue == 0) { return outParam.sValue; } else return null; } } })(); // Squished GUIDs are extremely little-endian (low nybble first) hex dumps of raw GUID structs: see // http://support.microsoft.com/kb/976220 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa373931 // http://www.symantec.com/connect/articles/brief-note-installer-guids function unsquish(squishedGuid) { return '{76543210-BA98-FEDC-HGJI-LKNMPORQTSVU}'.replace(/[0-Z]/g, function(d) { return squishedGuid.charAt(parseInt(d, 36)); }); } // Main productsKey = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products'; results = []; if (WScript.Arguments.length) { filter = new RegExp(WScript.Arguments(0), 'i'); products = reg.enumKey(reg.HKLM, productsKey); for (i = 0; i < products.length; ++i) { installPropertiesKey = productsKey + '\\' + products[i] + '\\InstallProperties'; name = reg.getStringValue(reg.HKLM, installPropertiesKey, 'DisplayName'); if (filter.test(name)) { package = reg.getStringValue(reg.HKLM, installPropertiesKey, 'LocalPackage'); results.push([products[i], unsquish(products[i]), package, name].join('\t')); } } } if (results.length) { WScript.Echo(results.join('\r\n')); } else { WScript.Quit(1); }