Home > Net >  How to refer to HKEY_CLASSES_ROOT in PowerShell?
How to refer to HKEY_CLASSES_ROOT in PowerShell?

Time:11-07

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.

CodePudding user response:

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 the PowerShell (Core) edition; it simply denotes a module that is at the core of either edition.

CodePudding user response:

Okay I figured out on my own,

checked Get-PSDrive

and saw the only registry aliases available by default on Windows/PowerShell are

HKCU                                   Registry      HKEY_CURRENT_USER
HKLM                                   Registry      HKEY_LOCAL_MACHINE

so, what I did, following this, was to add a new alias for HKEY_CLASSES_ROOT that is called HKCR

New-PSDrive -Name "HKCR" -PSProvider Registry -Root "HKEY_CLASSES_ROOT"
  • Related