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.
CodePudding user response:
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.
CodePudding user response:
Modified @Olaf solution with -ExpandProperty to handle long registry paths and Name not to have prefix before registry path
Get-ChildItem -Path 'HKCU:', 'HKLM:' -r -ea si |
Where-Object {
-not $_.SubKeyCount -and
-not $_.Property
} |
Select-object -ExpandProperty Name > list_empty.txt
