How to inventory installed software with powershell

I was asked to record the list of installed software on network computers.
Considering there are more than 20 computers. And I have other things to do, the reasonable choice would be to write a PowerShell script to do this for me.
I do prefer using SCCM to gather this info. This time, SCCM is not available.

From past experience, I know that Windows holds a list of all installed software in a place called the registry.
The right command to use is Get-ItemProperty. This command can read the registry and give me the information.

To the registry …

First, we need the path to the exact registry node that contains the installed software list.

We begin by navigating through the local registry:

  1. Start with “HKEY_LOCAL_MACHINE” or “HKLM”.
  2. Find the software key called “Software”.
  3. Look for Wow6432Node.
  4. This is Windows OS, we look in the “Microsoft Windows”.
  5. Our interest is localized to the “Current Version”,
  6. Finally, we find the list of the “Uninstallers”.

Therefore the command will look like this:


Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*

This is too much information.
We need to remove some of it. To do this we will use the Sort-Object. Let’s sort out by DisplayName
You can learn about how to use Sort-Object from Microsoft’s online documentation


Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Sort-Object DisplayName

Next, we need to choose what information we want to get from the registry.
We can specify the DisplayName, which is a friendly name that we can use to refer to it later.
Also filter by their Publisher, which is the name of the organization or person who published them.
Finally, we can filter by InstallDate, which is the date and time when they were installed.
By using these criteria, we can narrow down the list that matches our needs.

New Command will look now like this:


Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Sort-Object DisplayName | Select-Object DisplayName,Publisher,InstallDate

Finally, we need to format the results into a table. Then save the file on the server for processing.

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Sort-Object DisplayName | Select-Object DisplayName,Publisher,InstallDate | ft -AutoSize > \\FILESERVER\MISC\softwarelist.txt

Improving the script to handle multiple machines and users

Now we run into a few challenges, for example:
What if we have multiple users?
How do we prevent the file from being overwritten?
How do we know what computer has what software?

In order to accomplish this, the script has to run while the user is logging on to the system.
During the logon process, the script will have access to:
1. machine’s name
2. the username
This is how we going to achieve that:

First, we get Computer Name from the environment variable: $Env: Computername.
Next, we get the user name, yes you guessed right. Here is a variable for that too: $env: UserName.

 

The final script will look like this:


Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Sort-Object DisplayName | Select-Object DisplayName,Publisher,InstallDate | ft -AutoSize > \\FILESERVER\MISC\$env:ComputerName-$env:UserName-softwarelist.txt

Next is to process files and move the data to the SQL database. We will do that some other time. In meantime, you can check my other useful scripts like: List old files on disk using PowerShell