Search code examples
arrayspowershellkeyregistryuninstallation

Pulling a list of entries from Registry key and checking them for anything that is contained in an array


I am using the following code to try and pull a list of installed software on a system and check for certain entries within the list, so far I have managed to get the software list to run as desired using the following code:

$path = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' 
Get-ChildItem $path | Get-ItemProperty | Select-Object DisplayName
if (ItemProperty -Name -eq ('Wacom Tablet')) { 
  start notepad.exe
}

I would like this to be an array that references the DisplayName list, but I get the following error:

ItemProperty : Cannot find path 'C:\WINDOWS\system32\Wacom Tablet' because it
does not exist.
At C:\Users\username\Documents\Scripts\win10test.ps1:39 char:5
+ if (ItemProperty -Name -eq ('Wacom Tablet')) {
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\WINDOWS\system32\Wacom Tablet:String) [Get-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand

How could I achieve this?


Solution

  • ItemProperty is expanded to Get-ItemProperty, so your if condition

    ItemProperty -Name -eq ('Wacom Tablet')
    

    becomes

    Get-ItemProperty -Name -eq -Path ('Wacom Tablet')
    

    meaning that your code is trying to get a property -eq from an item Wacom Tablet in the current working directory (in your case apparently C:\WINDOWS\system32).

    What you seem to want to do is something like this:

    Get-ChildItem $path | Get-ItemProperty |
      Where-Object { $_.DisplayName -eq 'Wacom Tablet'} |
      ForEach-Object {
        # do stuff
      }
    

    or like this:

    $prop = Get-ChildItem $path | Get-ItemProperty |
            Select-Object -Expand DisplayName
    if ($prop -eq 'Wacom Tablet') {
      # do stuff
    }