Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically find a users HKEY_USERS registry key using powershell?

I wonder if there is a way to find a local user's registry key in HKEY_USERS if you know the login-name of that user on the local machine. I want to programmatically add stuff to a specific user's registry keys (Autorun for example), but I only know the username. How can I determine which of the cryptic users in HKEY_USERS actually belongs to a specific username?

like image 773
Erik Avatar asked Jun 06 '12 05:06

Erik


People also ask

How do I find registry keys in PowerShell?

One of the easiest ways to find registry keys and values is using the Get-ChildItem cmdlet. This uses PowerShell to get a registry value and more by enumerating items in PowerShell drives. In this case, that PowerShell drive is the HKLM drive found by running Get-PSDrive .

How do I browse the registry in PowerShell?

You can browse the registry tree the same way you navigate your drives. HKLM:\ and HKCU:\ are used to access a specific registry hive. Those, you can access the registry key and their parameters using the same PowerShell cmdlets that you use to manage files and folders.

What PowerShell command will list only the contents of a registry key or subkey?

Listing All Subkeys of a Registry Key PowerShell. Core\Registry , but this can be shortened to just Registry . Any of the following commands will list the contents directly under HKCU: . Get-ChildItem -Path Registry::HKEY_CURRENT_USER Get-ChildItem -Path Microsoft.


2 Answers

$User = New-Object System.Security.Principal.NTAccount($env:UserName)
$sid = $User.Translate([System.Security.Principal.SecurityIdentifier]).value

The above snippet gives you the SID of the logged-in user. This when appended to the HKEY_USERS givs you the right path for that username.

New-PSDrive HKU Registry HKEY_USERS
Get-Item "HKU:\${sid}"
like image 62
ravikanth Avatar answered Oct 20 '22 18:10

ravikanth


This answer is not complete, as HKEY_USERS does not contain all the users, just those that are currently active.

You'll need to load the registry hive for the user(s) you want to work with using

reg load hku\ThatUserName C:\Users\ThatUserName\NTUSER.DAT

See this SO answer for an example of how to load the registry hive for all the user(s).

You can then access the registry for that user with

Set-Location HKU:\ThatUserName

Or call New-PSDrive to give the user's registry it's own drive, like so:

New-PSDrive -Name HKThatUser -PSProvider Registry -Root HKU\ThatUserName 
Set-Location HKThatUser:

Be sure to unload the registry, and do garbage collection to ensure the hive is released when done:

reg unload hku\ThatUserName
[gc]::collect()

See this post for more info

like image 21
David Cobb Avatar answered Oct 20 '22 19:10

David Cobb