10
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.

2 Answers 2

12

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"
Sign up to request clarification or add additional context in comments.

1 Comment

This helped a lot. Added PSDrive aliases for: HKCR, HKCC, HKU. And if you're putting this in a script, you may wanna add some error handling: New-PSDrive -Name "HKCR" -PSProvider Registry -Root "HKEY_CLASSES_ROOT" -Scope Global -EA SilentlyContinue -EV EE_HKCR 1>$null This will both silence the SUCCESS stream (1) and adding any error to a custom error variable $EE_HKCR. And you want to make it Global to ensure it stays in your shell.
10

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.