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
$PSVersionTableI 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?
12 Answers
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 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, RootIt 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_MACHINENew-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 HKUThese 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_USERSTest-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>