Search code examples
powershellregistryacl

How do I take ownership of a registry key via PowerShell?


I'm attempting to take ownership of a registry key via PowerShell, and it's failing silently. I'm using the following code:

# get the username of the current user
$uname = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name

# create an identity reference for the owner
$user = new-object System.Security.Principal.NTAccount($uname)

# create a "drive" for HKEY_CLASSES_ROOT
new-psdrive -name HKCR_zf -psprovider Registry -root HKEY_CLASSES_ROOT

# change the current location
set-location HCKR_zf:\CLSID

# set ACLs
(get-acl '{E88DCCE0-B7B3-11d1-A9F0-00AA0060FA31}').setowner($user)

Everything runs fine up to the last line, but the .setowner() call fails silently (the owner is not changed).

How do I take ownership of a registry key?


Solution

  • Just calling SetOwner doesn't commit the change. You need to pass the acl object back to Set-Acl to commit the change.

    Check out this thread, there is a good description and full sample code. Seems there are some other hoops to jump through, as well:

    https://web.archive.org/web/20130812093535/http://social.technet.microsoft.com/Forums/windowsserver/en-US/e718a560-2908-4b91-ad42-d392e7f8f1ad/take-ownership-of-a-registry-key-and-change-permissions