Today I’m actually posting a little late, this usually goes out on Sunday but I bought a new server over the weekend and I got a little caught up in configuring it. Hopefully I can make it up to anyone that was anxiously anticipating my post with… My favorite function ever!
When I wrote this function I thought that maybe it would be better not to share it out as I had hoped to use it as the basis of an SCCM application. In the end I decided that it would best to share it freely, it’s not exactly a secret and it’s pretty useful on it’s own. PowerShellers have a pretty strong community largely because people share info freely. So enough back story and on to the function.
This week I’ll be talking about Get-ApplicationInfo, it originally started as Get-UninstallString but I renamed it to better fit it’s real purpose. Get-ApplicationInfo will query a windows computer and list off all of the installed applications. Quick note here, if an application is intentionally hiding itself, it will not be listed here. I don’t know anything other than Viruses that intentionally hide their installs but I’m putting the information out there. I don’t want to be accused of misleading anyone.
A lot of people talk about using WMI, and specifically the win32_product class to pull installed apps. Unfortunately win32_product will only poll for applications installed using a windows installer, msi file. Also on x64 systems it will miss x86 apps. I instead decided to write a function that queries the registry, specifically the Uninstall node under HKLM.
if you want to just grab the function and skip the article you can download it here.
How it works:
Get-ApplicationInfo has a couple neat features that I’m proud of, before I get onto the bells and whistles let me cover the core functionality – Querying the Registry
PowerShell makes querying the registry really easy, when you open up a powershell session it automatically loads up and “Mounts” two drives that expose the registry, HKLM and HKCU. They represent the Local Machine and Current user hives. When you write an application for windows you are supposed to ensure that your app contains a description of itself for add/remove programs as well as a set of instructions for how to remove it. The registry contains a node under, HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, that is supposed to contain all of that information.
* Quick note here, as I’ve discovered on my powershell journey supposed to and is are two different things. Give a developer some admin rights and they can do all kinds of crazy things…
So under that node each application writes a key with a whole set of properties. Those properties contain important information about the application and if we use PowerShell’s Get-ItemProperty Cmdlet on each of those keys we can filter through all of the properties and select the ones we want.
Get-ItemProperty ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*’ | Select -property Publisher,DisplayVersion,DisplayName,UninstallString
That command is the heart of get-applicationinfo, so now I can get onto the neat features.
x64 computers have a Wow6432node under software that we need to check. I added a get-wmiobject win32_operatingsystem version check to switch registry queries depending on the OS version. It helps illustrate the importance of creatively using if statements.
I added a -ping switch that will force the function to run, test-connection, before running the command. I often get annoyed by timeouts and I got into the habit of using test-connection inline with my scripts. The obvious problem with that being a large number of firewalls and workstations block icmp requests. I thought a ping switch was a nice compromise.
Ok well that’s all I have for this week, thanks for taking the time to read my post and as always please feel free to leave a comment. I’d like to know what I’m doing well and what I could be doing better. I’d also love to write an article that particularly addresses some reader’s problem or question. I’m no scripting guy but I like helping regardless.