Search code examples
powershellregistrywindows-11powershell-7.2

How to refer to HKEY_CLASSES_ROOT in PowerShell?


New-Item -Path "HKCR:\Directory\Background\shell\customname" -Force

I've been doing the same thing for HKCU and KHLM but when I try HKCR I get errors in PowerShell. how am I supposed to do it for HKEY_CLASSES_ROOT?

I searched for a solution but couldn't find any.


Solution

  • Defining a custom drive whose root is HKEY_CLASSES_ROOT, as shown in your own answer, is definitely an option, especially for repeated use.

    Ad hoc, you can alternatively use the Registry:: provider prefix directly with native registry paths:

    New-Item -Path 'Registry::HKEY_CLASSES_ROOT\Directory\Background\shell\customname' -Force
    

    Note:

    • The Registry part of the prefix is the provider name, as shown in Get-PSProvider's output.

    • Hypothetically, multiple providers with the same name could be registered, in which case you can prefix the name with the implementing module name for disambiguation; in the case of the registry provider, this module-qualified prefix is Microsoft.PowerShell.Core\Registry::[1] However, it's fair to assume that no third-party providers will choose a name that conflicts with the providers that ship with PowerShell, so Registry:: (or registry::, case doesn't matter), should do.

      • Note that the module-qualified provider name does show up in the prefix of the .PSPath property that provider items, such as reported by Get-Item and Get-ChildItem, are decorated with, e.g.:

        PS> (Get-Item HKCU:\Console).PSPath
        
        Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Console
        

    [1] Note that the Core part of the name does not refer to PowerShell Core (now renamed to PowerShell 7), the open-source, cross-platform PowerShell edition that succeeded Windows PowerShell; it simply denotes a module that is at the core of either edition.