Search code examples
powershellget-childitem

Get-ChildItem registry key, extract key name only


I can get a key in the Uninstalls key of the registry, for a specific installed program, like this...

$displayName = 'Parallels Tools'
$key = 'Registry::HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall'
Get-ChildItem -Path:$key | Where-Object {$_.GetValue('DisplayName') -eq $displayName}

... and the result will be a table, with a Name column showing just the GUID key name and a Properties column showing all the properties in the key. However, if I try to get just the GUID I run into problems. The column headers suggest (Get-ChildItem -Path:$key | Where-Object {$_.GetValue('DisplayName') -eq $displayName}).Name should work, but that shows the entire path, as does (Get-ChildItem -Path:$key | Where-Object {$_.GetValue('DisplayName') -eq $displayName}).ToString(). Is there some secret sauce happening with the dump to console? I know I can get to the GUID in a number of ways, Split-Path, or a Regex if I want to verify that the key is a GUID vs just a name. But I am curious if there is a way to leverage the same approach that dumping the object to the console is using?


Solution

  • Taking a look at the default formatter for RegistryKey objects, the Name column in the default view is populated using the .PSChildName property, which is the "leaf" of the full provider path.

    The "parent" portion of the provider path is stored in the .PSParentPath property.