In the previous tip we illustrated how powerful Get-ItemProperty is and that you can create an entire software inventory with just one line of code by reading multiple registry locations.

Today, let’s add two important pieces of information to our software inventory: the scope (was a software installed per user or for all users?) and the architecture (x86 or x64).

Each information is not found in any registry value but rather by determining where the information was stored inside the registry. And that’s another great benefit provided by Get-ItemProperty: it returns not just registry values of a given registry key. It also adds a number of properties like PSDrive (the registry hive) and PSParentPath (the path of the registry key being read).

And here is the solution that adds scope and architecture information to the software inventory based on where the information was found:

# Registry locations for installed software
$paths = 
  # all users x64
  'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*', 
  # all users x86
  'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*', 
  # current user x64
  'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*', 
  # current user x86
  'HKCU:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'

# calculated properties

# AllUsers oder CurrentUser?
$user = @{
    Name = 'Scope'
    Expression = { 
        if ($_.PSDrive -like 'HKLM')
        {
            'AllUsers'
        }
        else
        {
            'CurrentUser'
        }
    }
}

# 32- or 64-Bit?
$architecture = @{
    Name = 'Architecture'
    Expression = { 
        if ($_.PSParentPath -like '*\WOW6432Node\*')
        {
            'x86'
        }
        else
        {
            'x64'
        }
    }
}
Get-ItemProperty -ErrorAction Ignore -Path $paths |    
    # eliminate reg keys with empty DisplayName
    Where-Object DisplayName |
    # select desired properties and add calculated properties
    Select-Object -Property DisplayName, DisplayVersion, $user, $architecture




Twitter This Tip! ReTweet this Tip!

Anonymous