r/PowerShell • u/AzureSkye • Feb 26 '26
Question Getting registry keys with all subkeys and values, including empty?
Please help me find my mistake or point me in the right direction. 😞 I've been banging my head against this issue for a minute and I just keep working on other parts of my code, rather than addressing it.
Goal: I'm trying to selectively backup parts of the Windows Registry using Powershell functions, exporting to CliXML. Mostly, I'm backing up whole keys, though occassionally I'm only getting a single value. This is part of a non-Admin user transfer utility that I'm putting together to make it easier for my coworkers to switch to new computers.
Problem: How do I use a key's path to get every value and subkey's value, including empty/default? Get-Item doesn't recurse and Get-ChildItem doesn't give me the values in the top-level path, while neither gets me empty values.
Alternatives: I'm avoiding using reg export $path because I'm not keen on trying to combine text files correctly or handling errors from reg. I may be overthinking that, though. Also, I don't know if I even should worry about the empty keys...
Code:
(Note: Replace $Input with either $GI or $GCI. Not sure why ($GI, $GCI) doesn't give both results.)
$BackupKeys = @('HKCU:\Control Panel\PowerCfg', 'HKCU:\Control Panel\Appearance')
$GI = Get-Item -Path $BackupKeys
$GCI = Get-ChildItem -path $BackupKeys -depth 10
$Input | ForEach-Object { #Replace $Input with either $GI or $GCI. Not sure why ($GI, $GCI) doesn't give both results.
$key = $_ ;
$key.GetValueNames() | Select-Object `
@{ n='Path'; e={$key.ToString().replace('HKEY_CURRENT_USER', 'HKCU:')} },
@{ n='Name'; e={$_} },
@{ n='Type'; e={$key.GetValueKind($_)} },
@{ n='Value'; e={$key.GetValue($_)} }
}Missing key:
HKCU:\Control Panel\Appearance\New SchemesGet-Item result:
Path Name Type Value
---- ---- ---- -----
HKCU:\Control Panel\PowerCfg CurrentPowerPolicy String 0
HKCU:\Control Panel\Appearance SchemeLangID Binary {9, 4}
HKCU:\Control Panel\Appearance NewCurrent String
HKCU:\Control Panel\Appearance Current StringGet-ChildItem results:
Path Name Type Value
---- ---- ---- -----
HKCU:\Control Panel\PowerCfg\GlobalPowerPolicy Policies Binary {1, 0, 0, 0...}
HKCU:\Control Panel\PowerCfg\PowerPolicies\0 Description String This scheme is suited...
HKCU:\Control Panel\PowerCfg\PowerPolicies\0 Name String Home/Office Desk
HKCU:\Control Panel\PowerCfg\PowerPolicies\0 Policies Binary {1, 0, 0, 0...}
HKCU:\Control Panel\PowerCfg\PowerPolicies\1 Description String This scheme is designed...
HKCU:\Control Panel\PowerCfg\PowerPolicies\1 Name String Portable/Laptop
HKCU:\Control Panel\PowerCfg\PowerPolicies\1 Policies Binary {1, 0, 0, 0...}
...
HKCU:\Control Panel\Appearance\Schemes @themeui.dll,-850 Binary {2, 0, 0, 0...}
HKCU:\Control Panel\Appearance\Schemes @themeui.dll,-851 Binary {2, 0, 0, 0...}
HKCU:\Control Panel\Appearance\Schemes @themeui.dll,-852 Binary {2, 0, 0, 0...}
HKCU:\Control Panel\Appearance\Schemes @themeui.dll,-853 Binary {2, 0, 0, 0...}
HKCU:\Control Panel\Appearance\Schemes @themeui.dll,-854 Binary {2, 0, 0, 0...}
View all comments
3
u/omglazrgunpewpew Feb 26 '26
Sooo I feel like you're hitting a few registry weirdness things at once:
($GI, $GCI)gives you an array with two elements and each element is itself an array, soForEach-Objectiterates twice instead of over a flat list of keys. Use$GI + $GCIto concat them into one flat list, then you'll get both the top-level keys fromGet-Itemand the recursive kiddos fromGet-ChildItemin one pass..GetValueNames()doesn't include it, even when it's set. The default value's name is''(empty string), so you have to query it explicitly via.GetValue(''). Also, "default value exists but empty" and "default value doesn't exist" can look the same in output unless you distinguish$nullvs'', which matters for a real backup/restore.A little
try/catcharoundGetValueKindandGetValuewill also save you from edge-case keys that don't report cleanly.