2

I am using Windows 10 20H2, I have successfully recreated this problem multiple times without failure, on PowerShell Desktop 5.1.19041.610 and PowerShell Core 7.1.1.

The problem: Test-Path only works with two PowerShell registry drives(shortened registry hive names):

HKCU: and HKLM:

They are abbreviations of HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE respectively.

In PowerShell the full names of them are:

Registry::HKEY_CURRENT_USER and Registry::HKEY_LOCAL_MACHINE respectively.

However there are five registry hives, the other three being:

HKEY_CLASSES_ROOT, HKEY_CURRENT_CONFIG and HKEY_USERS

Their shortnames:

HKCR:, HKCC: and HKU:

Test-Path can't find the drives, however Test-Path can find them if full names are supplied:

TEST-PATH HKCR:
TEST-PATH HKCU:
TEST-PATH HKLM:
TEST-PATH HKU:
TEST-PATH HKCC:
TEST-PATH REGISTRY::HKEY_CLASSES_ROOT
TEST-PATH REGISTRY::HKEY_CURRENT_CONFIG
TEST-PATH REGISTRY::HKEY_USERS
$PSVersionTable

enter image description here

enter image description here

I am curious and I want to know why it behaves like this.

Can anyone offer an explanation?

I have confirmed the other three drives also don't work with other cmdlets, how can I use the three registry drives?

1

2 Answers 2

9

They are not created by default, but you can create them:

PS C:\>New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
HKCR                                   Registry      HKEY_CLASSES_ROOT

PS C:\>Test-Path HKCR:
True
PS C:>
1
  • I have HKCR: defined in my profile (with the output piped to Out-Null). I then also create acorr3espoinding function for each Registry drive using the same definition found in the existing drive letter functions: 'HKcR:','HKCU:','HKLM:' | ForEach { 'Function {0} {{ Set-Location $MyInvocation.MyCommand.Name }}' -f $_ | Invoke-Expression } Commented Jan 29, 2021 at 3:36
2

Yep, according to Microsoft Docs, PowerShell registry provider only provides two PSDrives by default:

HKCU: and HKLM:

They can be traversed like a filesystem.

To get all registry PSDrives:

Get-PSDrive -PSProvider Registry | Select-Object Name, Provider, Root

It will return something like this:

PS C:\Windows\System32> Get-PSDrive -PSProvider Registry | Select-Object Name, Provider, Root

Name Provider                           Root
---- --------                           ----
HKCU Microsoft.PowerShell.Core\Registry HKEY_CURRENT_USER
HKLM Microsoft.PowerShell.Core\Registry HKEY_LOCAL_MACHINE
New-PSDrive -PSProvider Registry -Root HKEY_CLASSES_ROOT -Name HKCR
New-PSDrive -PSProvider Registry -Root HKEY_CURRENT_CONFIG -Name HKCC
New-PSDrive -PSProvider Registry -Root HKEY_USERS -Name HKU

These will create PSDrives for the other three hives:

PS C:\Windows\System32> Get-PSDrive -PSProvider Registry | Select-Object Name, Provider, Root

Name Provider                           Root
---- --------                           ----
HKCC Microsoft.PowerShell.Core\Registry HKEY_CURRENT_CONFIG
HKCR Microsoft.PowerShell.Core\Registry HKEY_CLASSES_ROOT
HKCU Microsoft.PowerShell.Core\Registry HKEY_CURRENT_USER
HKLM Microsoft.PowerShell.Core\Registry HKEY_LOCAL_MACHINE
HKU  Microsoft.PowerShell.Core\Registry HKEY_USERS

Test-Path:

PS C:\Windows\System32> Test-Path HKCC:
True
PS C:\Windows\System32> Test-Path HKCR:
True
PS C:\Windows\System32> Test-Path HKCU:
True
PS C:\Windows\System32> Test-Path HKLM:
True
PS C:\Windows\System32> Test-Path HKU:
True
PS C:\Windows\System32>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .