Kae Travis

How to use Test-Path for HKEY_USERS

Posted on by in PowerShell

When using Test-Path to query the HKEY_USERS (or HKU) hive, it kept returning false even if the hive existed. This post explains how to use Test-Path for HKEY_USERS, and the modifications that we need to make.

How to use Test-Path for HKEY_USERS

Run the following command:

Get-PSDrive -PSProvider registry | select name, root

You’ll most probably find that you only have drives available for querying the HKLM and HKCU hives, but NOT the HKU (HKEY_USERS) hive.

In order to make this work we have two options. The first is to create a new drive and query it like so:

New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS
if (Test-Path "HKU:\S-1-5-21-1606980848-484061587-682003330-1613123") {
Write-Host "Found"
}

And the second is not to create a new drive, but to force Test-Path to use the registry provider like so:

if (Test-Path "Registry::HKU\S-1-5-21-1606980848-484061587-682003330-161123") {
Write-Host "Found"
} 
How to use Test-Path for HKEY_USERS
How to use Test-Path for HKEY_USERS

Leave a Reply