Gordon
Gordon

Reputation: 6873

Get-ChildItem filtered and sorted

I am trying to select all Uninstall keys in the registry that have a DisplayName property, sorted by Displayname. I would have thought this would work.

$uninstall32 = 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstall64 = 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstallKeys = (Get-ChildItem "Registry::$uninstall32" | Where-Object {$_.DisplayName} | sort DisplayName) +
                 (Get-ChildItem "Registry::$uninstall64" | Where-Object {$_.DisplayName} | sort DisplayName)

foreach ($uninstallKey in $uninstallKeys) {
    $uninstallKey
}

But that is returning nothing. If I remove the Where-Object I do get results, but not sorted. Where am I going wrong?

Upvotes: 0

Views: 2523

Answers (3)

js2010
js2010

Reputation: 27546

The output of get-childitem is kind of an illusion. It actually calls get-itemproperty in the format file. You need to use get-itemproperty to see values and data. You may want to use the get-package command instead. Note that Netbeans makes an invalid registry dword entry "NoModify" when it installs, that creates an exception in get-itemproperty.

Here's an approach with get-itemproperty:

get-itemproperty hklm:\software\microsoft\windows\currentversion\uninstall\* | 
  where displayname | sort displayname

Upvotes: 1

Theo
Theo

Reputation: 61208

If I understand the question correctly, you either want the key PATHS returned as a string array, OR the keys as Objects, including all properties:

$uninstall = 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*',
             'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'

$uninstallKeys = $uninstall | ForEach-Object {
    Get-ItemProperty "Registry::$_" | 
    Where-Object {$_.DisplayName} | 
    Sort-Object DisplayName | 
    # if you want the Keys Paths returned as string properties:
    Select-Object @{Name = 'RegistryKey'; Expression = {($_.PSPath -split '::')[1]}}

    # if you want the Keys returned with all properties as objects:
    # Select-Object $_.PSPath
}

$uninstallKeys

Upvotes: 0

Sage Pourpre
Sage Pourpre

Reputation: 10333

You can just pipe your Get-ChildItem commands into | Get-ItemProperty to obtain the desired result.

That being said, I encountered an issue where I had an invalid key in my registry. To circumvent that possible issue, I iterated into each items and got the property individually.

$uninstall32 = 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstall64 = 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstallKeys = (Get-ChildItem "Registry::$uninstall32") +
(Get-ChildItem "Registry::$uninstall64")

$AllKeys = `
  Foreach ($key in $uninstallKeys) {
  try {
    $Value = $Key | Get-ItemProperty -ErrorAction Stop
    $Value
  }
  catch {
    Write-Warning $_
  }
}
$AllKeys  = $AllKeys  | WHere DisplayName -ne '' | sort displayname

Reference

Regarding the potential Specified cast is not valid error with Get-ItemProperty & uninstall registry location

Upvotes: 1

Related Questions