Menu Close

CMD Batch – Registry Take Ownership, Backup ACL, Delete Key

Updated On January 13, 2019
You are here:
  • KB
  • Miscellaneous
  • CMD Batch - Registry Take Ownership, Backup ACL, Delete Key
< Back

Get the SetACL – https://helgeklein.com/setacl/ program.

Select 32/64 bit SetACL.EXE .

Create a new file RegOwnFix.BAT in the same folder.

Paste and save the code below.

Run RegOwnFix.bat as Admin.

Choose the settings (take, delete, backup).

Paste the complete key path that you want to take ownership.

@ECHO OFF
TITLE RegOwnFix

FOR /F "tokens=1,2*" %%V IN ('bcdedit') DO SET adminTest=%%V
IF (%adminTest%)==(Access) goto noAdmin
CD /D "%~dp0%"

SET _DEL=
SET _BKB=
SET _BKA=

FOR /f "tokens=2 delims==" %%a IN ('wmic OS Get localdatetime /value') DO SET "dt=%%a"
SET "YY=%dt:~2,2%" & SET "YYYY=%dt:~0,4%" & SET "MM=%dt:~4,2%" & SET "DD=%dt:~6,2%"
SET "HH=%dt:~8,2%" & SET "Min=%dt:~10,2%" & SET "Sec=%dt:~12,2%"
SET "BKF=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
SET BKReady=FALSE

:intro
CLS
ECHO ================================================================================
ECHO RegOwnFix - DIY Registry Permission Unfuckery Tool
ECHO ================================================================================
ECHO Settings:
ECHO   [X] Take registry key ownership and full control (always on)
IF "%_DEL%"=="" ( ECHO   [ ] 1. Delete key after ownership )  ELSE ( ECHO   [X] 1. Delete key after ownership )
IF "%_BKB%"=="" ( ECHO   [ ] 2. Backup key before ownership ) ELSE ( ECHO   [X] 2. Backup key before ownership )
IF "%_BKA%"=="" ( ECHO   [ ] 3. Backup key after ownership )  ELSE ( ECHO   [X] 3. Backup key after ownership )
ECHO.
ECHO Type X to close the tool.
CHOICE /C 123SX /D S /T 10 /N /M "Type 1/2/3 to change settings or press S to start. (default S in 10 secs)"
IF %ERRORLEVEL% EQU 3 GOTO setBKA
IF %ERRORLEVEL% EQU 2 GOTO setBKB
IF %ERRORLEVEL% EQU 1 GOTO setDEL
IF %ERRORLEVEL% EQU 4 GOTO setup
IF %ERRORLEVEL% EQU 5 GOTO end
GOTO end

:setup
IF "%_BKB%"=="X" ( GOTO mkBKPFolder )
IF "%_BKA%"=="X" ( GOTO mkBKPFolder )
:rtnmkBKP
GOTO askkey

:askkey
SET _reg=""
ECHO.
ECHO Paste the complete key address to take ownership or leave empty to end.
REM ECHO Press ENTER to continue
SET /P _reg=Enter the next key:
IF "%_reg%"=="""" ( GOTO end )
set _reg=%_reg:Computer\=%
set _regf=%_reg:\=_%
set _regf=%_reg:\=!%
GOTO runacl

:runacl
ECHO ________________________________________________________________________________
ECHO Starting...
IF "%_BKB%"=="X" ( GOTO doBKB )
:rtnBKB
ECHO  - Taking ownership.
SetACL.exe -ot reg -actn setowner -ownr "n:Administrators" -on "%_reg%"
SetACL.exe -ot reg -actn ace -ace "n:Administrators;p:full" -on "%_reg%"
IF "%_BKA%"=="X" ( GOTO doBKA )
:rtnBKA
IF "%_DEL%"=="X" ( GOTO doDEL )
:rtnDEL
ECHO Done.
ECHO ________________________________________________________________________________
GOTO askkey

:setDEL
IF "%_DEL%"=="" ( SET _DEL=X) ELSE ( SET _DEL=)
GOTO intro

:setBKB
IF "%_BKB%"=="" ( SET _BKB=X) ELSE ( SET _BKB=) 
GOTO intro

:setBKA
IF "%_BKA%"=="" ( SET _BKA=X) ELSE ( SET _BKA=) 
GOTO intro

:doDEL
ECHO  - Deleting the key.
REG DELETE "%_reg%" /f
GOTO rtnDEL

:doBKB
ECHO  - Backup before.
POWERSHELL "Get-Acl -Path Registry::%_reg: =` % | Format-List" > ".\%BKF%\Before_%_regf%.acl.txt"
REG EXPORT "%_reg%" ".\%BKF%\Before_%_regf%.reg" /y
GOTO rtnBKB

:doBKA
ECHO  - Backup after.
POWERSHELL "Get-Acl -Path Registry::%_reg: =` % | Format-List" > ".\%BKF%\After_%_regf%.acl.txt"
REG EXPORT "%_reg%" ".\%BKF%\After_%_regf%.reg" /y
GOTO rtnBKA

:mkBKPFolder
IF "%BKReady%"=="TRUE" ( GOTO setup )
SET BKReady=TRUE
mkdir .\%BKF%
GOTO rtnmkBKP

:bkinfo
ECHO.
ECHO Your backup files are ready at:
ECHO %cd%\%BKF%
GOTO rtnbkinfo

:noAdmin
echo Current user permissions to execute this .BAT file are inadequate.
echo This .BAT file must be run with administrative privileges.
echo Exit now, right click on this .BAT file, and select "Run as administrator".  
pause >nul

:end
IF "%BKReady%"=="TRUE" ( GOTO bkinfo )
:rtnbkinfo
SET _reg=
SET _regf=
SET BKF=
SET BKReady=
SET BKInfo=
SET _DEL=
SET _BKB=
SET _BKA=
SET dt=
SET YY=
SET YYYY=
SET MM=
SET DD=
SET HH=
SET Min=
SET Sec=
ECHO.
TITLE Command Prompt
ECHO ============================== RegOwnFix  Closing ==============================

Comments are welcome.

%d bloggers like this: