r/PowerShell • • 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 Schemes

Get-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            String

Get-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...}
15 Upvotes

12 comments sorted by

View all comments

3

u/omglazrgunpewpew Feb 26 '26

Sooo I feel like you're hitting a few registry weirdness things at once:

  1. ($GI, $GCI) gives you an array with two elements and each element is itself an array, so ForEach-Object iterates twice instead of over a flat list of keys. Use $GI + $GCI to concat them into one flat list, then you'll get both the top-level keys from Get-Item and the recursive kiddos from Get-ChildItem in one pass.
  2. For default (unnamed) value: .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 $null vs '', which matters for a real backup/restore.

A little try/catch around GetValueKind and GetValue will also save you from edge-case keys that don't report cleanly.

1

u/AzureSkye Feb 26 '26
  1. Oh wow, I'm a friggin' idiot. I hadn't considered just appending the variables.
  2. Okay, that's super helpful to know! It sounds like I'm trying to be too clever again and I should just rely on reg export.

1

u/omglazrgunpewpew Feb 27 '26

Yeah, you weren’t crazy, you were just inadvertently building an array-of-arrays.

If your end goal is a reliable restore on a new machine, lean on reg export for that. It’s boring in the best way and preserves exact reg semantics. For the few “single value only” cases, handle those separately, your current CliXML object approach is great for that.