0

So I am trying to find installed software using the uninstall registry keys but want to only display software by certain publishers. In my array of publishers to look for I want to use wild cards. I can get this to work with only one publisher but if I have multiple publishers it returns nothing. I have tried everything I could think of and I search for on this site and others but I am not sure why it won't work.

I have tried it with and without the last for each statement with the same results. I feel like this should be a simple fix. Here is the code:

$HKLM = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
$HKLM_Wow = "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
$HKCU = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
$InstalledSoftware = Get-ChildItem $HKLM, $HKLM_Wow, $HKCU
$allSoftware = [System.Collections.ArrayList]@()
$SoftwarePub =  [System.Collections.ArrayList]@("Microsoft*", "Dell*")

foreach($obj in $InstalledSoftware) {
    $software = New-Object -TypeName PSObject
    $software | Add-Member -MemberType NoteProperty -Name DisplayName -Value            $obj.GetValue("DisplayName")
    $software | Add-Member -MemberType NoteProperty -Name Version -Value $obj.GetValue("DisplayVersion")
    $software | Add-Member -MemberType NoteProperty -Name Publisher -Value     $obj.GetValue("Publisher")
    $software | Add-Member -MemberType NoteProperty -Name InstallDate -Value        $obj.GetValue("InstallDate")
    $null = $allSoftware.Add($software)
}
foreach ($pub in $SoftwarePub)
{
 $allSoftware | Where-Object {$_.Publisher -like $SoftwarePub}
}
JohnA2023
  • 1
  • 1
  • see ready to use script https://powershell.one/code/12.html – MikeSh Jul 06 '23 at 15:06
  • 1
    `{$_.Publisher -like $Pub}`? – JosefZ Jul 06 '23 at 15:10
  • @JosefZ that was it when I added the foreach I didn't change that and have been beating my head against the wall since yesterday. Thank you! – JohnA2023 Jul 06 '23 at 15:51
  • Glad to hear that JosefZ spotted your typo. As for your original intent to match against _multiple_ wildcard patterns: see [this answer](https://stackoverflow.com/a/74379608/45375). – mklement0 Jul 07 '23 at 01:19
  • As an aside: Unless you're still using PSv2, a much simpler and more efficient way to construct custom objects is to use the following syntactic sugar: `[pscustomobject] @{ foo = 1; bar = 'two' }` - see [this answer](https://stackoverflow.com/a/45293404/45375) – mklement0 Jul 07 '23 at 01:21

1 Answers1

0

Having read the feedback left on my previous response, I looked into your script, made a couple changes and got the below to work:

$SoftwarePub = @("Microsoft", "Dell")

$HKLM = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
$HKLM_Wow = "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
$HKCU = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"

$registryKeys = Get-ChildItem $HKLM, $HKLM_Wow, $HKCU -ErrorAction SilentlyContinue

$installedSoftware = foreach ($key in $registryKeys) {
    $publisher = $key.GetValue("Publisher")
    if ($SoftwarePub | Where-Object { $publisher -like "*$_*" }) {
        [PSCustomObject]@{
            DisplayName = $key.GetValue("DisplayName")
            Publisher = $publisher
            Version = $key.GetValue("DisplayVersion")
            InstallDate = $key.GetValue("InstallDate")
        }
    }
}

$installedSoftware | Select-Object DisplayName, Publisher, Version, InstallDate

enter image description here

Nuno Chaves
  • 176
  • 3
  • [Please *Stop Using `Win32_Product` To Find Installed Software*.](https://xkln.net/blog/please-stop-using-win32product-to-find-installed-software-alternatives-inside/) – JosefZ Jul 07 '23 at 15:26