PowerShell Problem Solver: Find Installed Software Using PowerShell

Posted on November 19, 2014 by Jeff Hicks in PowerShell with 0 Comments

aOne question that I see often in PowerShell forums is how to find out what applications are installed on a given computer. Naturally the solution should be done with PowerShell. This is a great choice because as you will see, identifying installed applications is pretty easy and once you have the information you can do much with it from exporting to a CSV file to creating an HTML report.

One thing I wouldn’t do though is to rely on scripting techniques, PowerShell or otherwise to service as a license management mechanism. If you have that type of business requirement then you should be investing in a license management product. My other caveat is that you can really only find applications that were installed via an MSI package. It is very hard to find any stand-alone executables unless you know what you are looking for. But let’s see what we can figure out.

The primary tool will be WMI, and specifically the Win32_Product class. You can query this class with Get-WMIObject. But if you are running PowerShell 3.0 or later, I recommend you use Get-CimInstance. You will be querying the same WMI class, but Get-WMIObject relies on RPC and DCOM connections whereas Get-CimInstance uses the WSMan protocol, which makes it much more firewall friendly. Although there may be situations where you want to take advantage of both. For the most part you can interchange the cmdlets in my examples, except where noted.

First, let’s look at the class.

I’m using the [wmiclass] type accelerator to define a variable to hold information about the class. I can use that to examine the properties.

Win32_Product properties. (Image Credit: Jeff Hicks)

Win32_Product properties. (Image Credit: Jeff Hicks)

I can do something similar to discover methods.

Win32_Product methods. (Image Credit: Jeff Hicks)

Win32_Product methods. (Image Credit: Jeff Hicks)

But it is a bit harder I think to really understand these methods. Intead, let’s use the CIM cmdlets to look at the class.

Not too much difference when it comes to displaying properties. But identifying class methods is much easier.

Identifying class methods with PowerShell. (Image Credit: Jeff Hicks)

Identifying class methods with PowerShell. (Image Credit: Jeff Hicks)

If you want to use Out-Gridview, you need to take an extra step to get the parameters formatted properly.

Formatting parameters. (Image Credit: Jeff Hicks)

Formatting parameters. (Image Credit: Jeff Hicks)

Now that we know what we have to work with let’s see what is installed. I’m going to test locally and begin with a simple query.

This command will return instance of the Win32_Product class on my computer. The reason I’m using a job is because it has always been my experience that querying this class takes a long time. If you want to do something similar with Get-CIMInstance, then you will need to use Start-Job because Get-CimInstance doesn’t have an –AsJob parameter.

If you have to query multiple remote computers, one approach might be to use remote PSSessions in combination with these cmdlets. In the two examples above, the job is running on my computer. Here’s an alternative. First, create your remoting sessions.

Then you can use Invoke-Command to start a background job on the remote computers.

Now each computer is running its own WMI job, and I can come back later to retrieve results. In PowerShell v3 and later I could also take advantage of disconnected sessions. But back to my local job. It has finished, so I’ll get the results.

Looking at the count property, I see 200 installed products.

I should point out that what gets registered with Win32_Product is more than just a product like Microsoft Outlook. A number of applications, especially Microsoft Office, install a number of other related “products” that you may never interact with directly.

Sponsored

But what type of information did I get? Since we’re exploring something new, I’ll look at a single object.

If you use Get—CimInstance, then you will get a slightly different display. But as with most things in PowerShell, there is always more than meets the eye so we should look at all properties.

Or a variation to omit the system properties:

If you use Get-CIMInstance, the system properties are suppressed by default. But in any event you should see something like this:

Using Get-CIMInstance in PowerShell. (Image Credit: Jeff Hicks)

Using Get-CIMInstance in PowerShell. (Image Credit: Jeff Hicks)

Once I know what kind of data I can get, it becomes much easier to display relevant results.

I’m only displaying some key properties.

Displaying key properties. (Image Credit: Jeff Hicks)

Displaying key properties. (Image Credit: Jeff Hicks)

With Out-Gridview I could do further filtering and sorting. Remember, we have PowerShell objects here so I can do just about anything I want with them. Perhaps export all data to a CSV file.

Exporting data to a CSV file. (Image Credit: Jeff Hicks)

Exporting data to a CSV file. (Image Credit: Jeff Hicks)

One change I made was to add the PSComputername property to my list, so I would know where these products are installed. I also used –NoTypeInformation because I intend to use the CSV file outside of PowerShell.

Or perhaps I’d like to group.

Grouping data by vendor. (Image Credit: Jeff Hicks)

Grouping data by vendor. (Image Credit: Jeff Hicks)

Perhaps I want to identify all non-Microsoft products. I can do that with regular expressions.

Identifying non-Microsoft products. (Image Credit: Jeff Hicks)

Identifying non-Microsoft products. (Image Credit: Jeff Hicks)

If you know what you are looking for from the very beginning, then you can use a WMI filter. For example, perhaps all I want to know from the start are non-Microsoft products. Here’s how I could accomplish that:

Or I want to see if Google Chrome is installed:

Sponsored

WMI filters use the legacy operators. You should always try to filter this way instead of getting everything and then filtering with Where-Object.

Although it is perfectly acceptable to query multiple machines and then filter the aggregated results.

One final note is that if you filter for something and nothing matches, you won’t get anything back, nor will you get an error. You will need to use something like an IF statement in your expression. There are so many possibilities with this class. I’ve tried to highlight a few key ones. But if there is something you are curious about, please post in the PowerShell forum here on Petri.com. I will be revisiting this topic in future articles.

Sponsored

Tagged with ,