Script Tips

A Quick Software Inventory Script

Use a script to find out who's installed what software

Software inventory is a very common request in the world of desktop management. Most environments have some sort of asset management system in place that reports on many things, including software. This week, I show you a script that can do basic inventory or perform an operation based on whether certain software is installed.

Rather than digging through the file system looking for known file names or file version information, the quick and easy way to get that info is to enumerate the subkeys in the Uninstall subkey of the registry. That is, you're basically retrieving a list of each subkey in that level of the registry structure. Once you have that info, you can dynamically build a list of what's installed by looking for some common registry values describing the installed items. What you'll end up with will look suspiciously like what you'd see if you brought up the Add/Remove Programs applet (that's because the script here and the ARP work off of the same data).

While KiXtart and AutoIt are nice enough to provide nice enumeration functions for the registry, the Windows Script Host leaves us hanging in this regard. Luckilly, there is always more than one way to skin a cat and this time we'll call on WMI to provide with some similar functionality. WMI uses hexadecimal values to identify what hive in the registry you're looking into, so let's start by setting a few constants (unchangeable variables) to make the script easier to read:

Const HKCR = &H80000000
Const HKCU = &H80000001
Const HKLM = &H80000002

Next, you'll need to get a handle on the WMI registry provider, which you do like so:

Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
   ".\root\default:StdRegProv")

I know, it looks ugly, but that's what VBscript damands of us sometimes. Now, set the path you want to enumerate:

sUninstallPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

And finally, you can now enumerate all the subkeys beneath this registry path in the HKEY_LOCAL_MACHINE hive and store them in a variable named aSubkeys:

oReg.EnumKey HKLM, sUninstallPath, aSubkeys

The aSubkeys variable is an array, so you'll run through each value it has using a For/Each loop. The GetStringValue method of the WMI registry provider is used here to build the path in the registry to each subkey of the Uninstall subkey, and inside this key is where you collect the value of "DisplayName" in a variable named sAppName. Finally you echo each value:

On Error Resume Next
For Each sSubkey In aSubkeys
   oReg.GetStringValue HKLM, sUninstallPath &"\" &_
      sSubkey, "DisplayName", sAppName
   WScript.Echo sAppName
Next

You'll note I've added the statement, "On Error Resume Next" here -- this is to ensure the script does not produce an error if it looks inside one of these subkeys and does not find a value named "DisplayName" (which does happen for some applications that don't like to follow the rules).

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at editor@mcpmag.com; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

Want to inventory a remote system? If you have admin access to read their registry remotely, you can replace the period in the line where oReg (which means local system) is set to the name of a remote system. Here's an example where a computer named COMPUTERNAME is inventoried:

Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
   "COMPUTERNAME\root\default:StdRegProv")

There you go! Just a dozen lines of code (and you could easily reduce even this) and you have yourself a nice little software inventory of the local machine. I thought this was a good way to cover how to enumerate subkeys in the registry with VBScript, and now you have a nice useful example while you're at it!

About the Author

Bob Kelly is president and co-founder of AdminScriptEditor.com, home to an integrated suite of scripting tools and a shared library of scripts and language help. He has authored books on scripting and desktop administration and several white papers. Bob also owns and operates AppDeploy.com, where he writes and produces videos on topics related to software deployment.

comments powered by Disqus
Most   Popular

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.