Search code examples
windowspowershellscriptingregistry

Partial match of application DisplayNames in Windows Registry?


I'm a PowerShell noob. Sorry in advance. I'm trying to write a quick script that will check a PC to see if a list of applications are installed. I'm storing the list in an array, and comparing it to the DisplayName of apps in the Uninstall keys in the registry.

$Apps=@('Paint.net', 'Blender', 'SketchUp')

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
  Where-Object {$Apps -Like $_.DisplayName} | 
  Select-Object DisplayName, DisplayVersion

This works, but only for exact matches. "Blender" and "Paint.net" are listed in the output, but not SketchUp, because the full DisplayName is "SketchUp 2017". How do I get this to match keywords regardless of the other text (version numbers) in the DisplayName? I've tried wildcards around the $_.DisplayName property, but they either throw an error or don't return anything at all. I'd rather not have to update the script every time Audacity's version clicks up a couple of notches.

This is probably something simple, like expanded properties or something, but it is beyond my grasp. Your help is appreciated.


Solution

  • What you can do is make a Regular Expression out of your $Apps array, and use -match against that. You will need to escape your strings to account for any special characters, and join them with a pipe (which in RegEx is the or operator).

    $Apps=@('Paint.net', 'Blender', 'SketchUp')
    $AppsRegEx = ($Apps | ForEach{[regex]::escape($_)}) -join '|'
    
    Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
      Where-Object {$_.DisplayName -match $AppsRegEx} | 
      Select-Object DisplayName, DisplayVersion
    

    That should match keywords for you.