Search code examples
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

    • Assuming that a given registry key exists, it may or may not have an unnamed value, which regedit.exe represents as (Default) in its GUI.

    • Similarly, the GUI showing a Data value of (Value not set) implies that this value doesn't exist, so there's nothing to retrieve.

    • If the unnamed value does exist, you can retrieve its value:

      • Using the Get-ItemPropertyValue PowerShell cmdlet:

        • Refer to it by literal string '(Default)' (irrespective of what the Windows display language is) as the -Name argument.

          # NOTE: FAILS, if the unnamed value doesn't exist.
          Get-ItemPropertyValue -Path 'HKCU:\AppEvents' -Name '(Default)'
          
        • As an aside: Due to a bug, still present as of PowerShell (Core) 7 v7.4.x, Remove-ItemProperty does not recognize '(Default)' - see GitHub issue #6243.

      • Alternatively, using the .NET .GetValue() method of the [Microsoft.Win32.RegistryKey] type, in combination with Get-Item, which outputs such instances (when targeting the registry).:

        • Use '', i.e. the empty string:

          # NOTE: Returns $null, if the unnamed value doesn't exist.
          (Get-Item -Path 'HKCU:\AppEvents').GetValue('')
          
        • If the unnamed value exists, it is also returned as a '' element from the .GetValueNames() method.


    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('')