Search code examples
powershellregistry

Find and display registry empty keys with Powershell


How to find and display Windows registry empty keys using Powershell?

Know that GetChild-Item can search Registry using HKLM:\ or HKCU:\.

I tried

Get-ChildItem "HKLM:\" -Recurse | 
    Where Property -like '' | foreach {
    $_.GetValue("DisplayName")
}

but doesn't work.

For empty keys I mean keys that have only the Default value and do not have sub-keys.

Then export to a text file using >, analyze the result and delete what is not needed.


Solution

  • So you're looking for registry keys with no subkey and no property - except for the default value. Because both properties are provided when you use Get-ChildItem you just have to filter for them:

    Get-ChildItem -Path 'REGISTRY::HKEY_LOCAL_MACHINE', 'REGISTRY::HKEY_CURRENT_USER' -Recurse -ErrorAction SilentlyContinue |
        Where-Object {
            -not $_.SubKeyCount -and
            -not $_.Property
        } | 
            Select-object -Property PSPath
    

    It is still a bad idea to remove keys from the registry without the need.