windowspowershellregistrydefault

Windows PowerShell Get Registry '(Default)' 'Value Size' and 'Value data'


If I use regedit, I can view Registry 'Computer\HKEY_CURRENT_USER\AppEvents'. The only entry is: Name (Default) Type 'REG_SZ' Data (Value not set)'

If I double-click on the '(Default)' entry: Value name: (Default) Value data: <blank>

I've tried numerous ways to access the 'Type' and 'Data' contents without success.

I've tried using:

(1) $key = Get-Item -Path 'HKCU:\AppEvents' 
(2) foreach ($name in $key.GetValueNames())  

but this either results in an exception or doesn't get the values.

I've found nothing on the web that has a simple explanation of how to access the '(Default)' type and data values.

Anyone who can provide a simple way to do this? I've tried many ways to get the name and data values.


Solution


  • If you want to quietly default to $null if the unnamed value doesn't exist:

    # Using Get-ItemPropertyValue.
    # NOTE: Also ignores the nonexistence of the *key*.
    $unnamedValueDataIfAny =
      try { Get-ItemPropertyValue -Path 'HKCU:\AppEvents' '(Default)' -ErrorAction Stop } 
      catch { $null }
    
    # Using .NET.
    # NOTE: Reports an *error* in the event of nonexistence of the *key*.
    $unnamedValueDataIfAny =
      (Get-Item -Path 'HKCU:\AppEvents').GetValue('')