@echo off
REM Edited 28th Mar 2023 to cover Win11 and above.
REM Updated to allow ERRORLEVEL return codes. Also make easier to use as a
REM standalone system.  Make sure you run as Administrator or certain parts
REM will NOT run properly. Added UserTweaksFiles folder to keep things tidy.
REM Place all your .reg etc files within this folder and use %UTLoc% variable
REM within this script to be the location of your .reg/etc files.
CLS
TITLE UserTweaks Installation In Progress - Please Wait...
mode con cols=86 lines=10
COLOR 1F
echo.
REM     -------------------------------------------
REM     UserTweaks.cmd  !!DO NOT RENAME this file!!
REM     -------------------------------------------
REM -
REM     Make sure you place your tweaks within the sections for the correct
REM     Windows version ie Win1x , Win7 etc.
REM -
REM     This Add-On was created to help you add your own tweaks easier.
REM -
REM     When using registry (.reg) files use the following filenames:
REM -
REM     For Windows 1x:
REM     W10CU.reg, W11CU.reg or W12CU.reg for Current User entries.
REM     W10LM.reg, W11LM.reg or W12LM.reg for Local Machine entries.
REM -
REM     For Windows 8:
REM     W8CU.reg for Current User entries.
REM     W8LM.reg for Local Machine entries.
REM -
REM     For Windows 8.1:
REM     W81CU.reg for Current User entries.
REM     W81LM.reg for Local Machine entries.
REM -
REM     For Windows 7:
REM     W7CU.reg for Current User entries.
REM     W7LM.reg for Local Machine entries.
REM -
REM     For Windows Vista:
REM     WVCU.reg for Current User entries.
REM     WVLM.reg for Local Machine entries.
REM -
REM     For Windows 1x Preview specific:
REM     W10PCU.reg, W11PCU.reg or W12PCU.reg for Current User entries.
REM     W10PLM.reg, W11PCU.reg or W12PCU.reg for Local Machine entries.
REM -
REM     %UTLoc% is the Tweaks directory, DO NOT ALTER!
REM     ---------------------------------------------
REM -
REM     Place your CORRECTLY named .reg files within the UserTweaksFiles folder,
REM     then place the UserTweaks.cmd and the UserTweaksFiles folder within the
REM     $oem$\$$\Setup\Scripts (so it looks like $oem$\$$\Setup\Scripts\UserTweaksFiles)
REM     fodler where the DeCompile.exe, OOBE.cmd etc is located. You can have
REM     Vista/7/8.x/1x .reg files in same folder as only the OS that is installed
REM     will the correct registry files will be chosen to be used, ie Win7 has been
REM     installed only W7CU and W7LM reg files used all other .reg files will be
REM     ignored. For registry key ownership also copy over both the SetACLxXX.exe
REM     files to cover 32/64 bit OS's. The 32bit version works on 64 bit but it
REM     may not always 'set' certain 64 bit reg entries correctly.
REM -
REM     You can add 'REG ADD' etc to the sections if you wish, just do NOT invoke
REM     a system reboot, or your tweaks may fail to work. If you need to reboot,
REM     do so manually after the desktop has appeared for the first time.
REM -
REM     Please Note: Some registry entries may not work due to OS permissions on the particular
REM               registry key.
REM -
REM     To give registry permission to a certain key and SETACLxXX has been added then use:
REM -
REM      CALL :RegOwn "<registry key to take control of>" Note the Quotes around the parameter.
REM -
REM     For example:
REM -
REM      CALL :RegOwn "HKLM\SOFTWARE\Microsoft\Internet Explorer"
REM -
REM     This gives you control of the given registry key, it does NOT reset the permission
REM     after.  Please be very careful of which key(s) you take control of - See Readme file.
REM -
REM     If using the :RegOwn subroutine, do them here BEFORE the SET MRPHKCU/MRPHKLM lines or
REM     your tweaks that require ownership will not take place.
REM -
REM    Avoid using any programs that require user input or use any ECHO commands as the script
REM    is run under SYSTEM control and there may NOT be any visible output.  If you do use any
REM    programs make sure they are SILENT installers that do NOT ask any input from the user or
REM    results may not be as you expect! ie the script may 'hang' as nothing is seen on the screen!
REM    The script was designed for simple .reg files as mentioned above, or REG ADD type commands.
REM -
REM     Disclaimer: Any tweaks or registry changes you do are at YOUR OWN RISK!
REM                 There will be NO support given for anything you add to this script.
REM                 This script is provided as-is, if you use it and it works great, if it not
REM                 work then MRP is NOT responsible for YOUR mistakes!
REM     ==========================================================================================

REM ** Avoid editing the script below this line, you can edit where indicated **

REM Location of Tweaks Files.... Do NOT change unless you know what you are doing!
SET "UTLoc=%WinDir%\Setup\Scripts\UserTweaksFiles"

REM For StandAlone use, remove REM from the line below.

REM SET "UTLoc=%~dp0UserTweaksFiles"

REM ==========================================================================================
SET "INL=IF NOT ERRORLEVEL 1"
SET "ErrCode=0"

REM Set variables.
SET RQR1=REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "ProductName"
SET RQR2=REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "EditionId"
SET RQR4=REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "DrvType"

:SSDDetect
REM Simple SSD check {Experimental}.
REM If SSD then Set to YES, if normal HDD then sets to NO.

SET "SSD=NO"
IF EXIST "%UTLoc%\IFSSD.exe" (
CALL "%UTLoc%\IFSSD.exe" %SystemDrive%
TIMEOUT /T 2 /NOBREAK >nul
DEL /F /Q "%UTLoc%\IFSSD.exe" >NUL 2>&1
)

%RQR4% | findstr /I "\<DrvType\>" | findstr /I "\<SSD\>" >nul && %INL% SET "SSD=YES"

REM Example SSD tweaks, remove REM from begining to use.

GOTO :SkipSSD

IF /I "%SSD%"=="YES" (
    Echo SSD Tweaks...
    sc config SysMain start= disabled >NUL 2>&1
    reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Power" /v "HiberbootEnabled" /t REG_DWORD /d 0 /f >NUL 2>&1
    reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" /v "DisablePagingExecutive" /t REG_DWORD /d 1 /f >nul
    reg add "HKLM\SYSTEM\CurrentControlSet\Control\WMI\Autologger\ReadyBoot" /v "Start" /t REG_DWORD /d "0" /f >nul
    reg add "HKLM\Software\Microsoft\Windows\CurrentVersion\OptimalLayout" /v "EnableAutoLayout" /t REG_DWORD /d 0 /f >NUL 2>&1
    Reg add "HKLM\SOFTWARE\Microsoft\Dfrg\BootOptimizeFunction" /v "OptimizeComplete" /t REG_SZ /d N /f >nul
    reg add "HKLM\Software\Microsoft\Dfrg\BootOptimizeFunction" /v "Enable" /t REG_SZ /d "N" /f >NUL 2>&1
)

:SkipSSD
IF /I "%SSD%"=="NO" (
     powercfg -h on >NUL 2>&1
     powercfg /hibernate /type reduced >NUL 2>&1
     reg add "HKLM\System\CurrentControlSet\Control\Session Manager\Power" /v "HiberbootEnabled" /t REG_DWORD /d "1" /f >NUL 2>&1
     echo Reduced space for Fast Boot tweak for Standard Hard Drives Applied.
)


:OSARCHSTATUS
REM ::Set OS arch 64 or 32 bit.
SET "OSARCH=x86"
SET "bits="
FOR /f "tokens=2 delims==" %%a in ('wmic os get osarchitecture /value 2^>nul') do set "bits=%%a"
ECHO %bits% | FINDSTR "64" >NUL 2>&1 && SET "OSARCH=x64"
ECHO %bits% | FINDSTR "32" >NUL 2>&1 && SET "OSARCH=x86"
IF /I "%OSARCH%"=="x64" GOTO :DoneArchCheck

IF DEFINED bits IF /I "%bits%"=="64-Bit" SET "OSARCH=x64"
IF DEFINED bits IF /I "%bits%"=="32-Bit" SET "OSARCH=x86"
IF /I "%OSARCH%"=="x64" GOTO :DoneArchCheck

REM Recheck if x86 to make sure it's correct.
ECHO %PROCESSOR_ARCHITECTURE% | FINDSTR "64" >NUL 2>&1 && SET "OSARCH=x64"
ECHO %PROCESSOR_ARCHITECTURE% | FINDSTR "86" >NUL 2>&1 && SET "OSARCH=x86"
IF /I "%OSARCH%"=="x64" GOTO :DoneArchCheck

SET "OSARCH=x64"
IF /I "%PROCESSOR_ARCHITECTURE%"=="x86" IF "%PROCESSOR_ARCHITEW6432%"=="" SET "OSARCH=x86"
IF /I "%OSARCH%"=="x64" GOTO :DoneArchCheck

IF EXIST "%systemdrive%\Program Files (x86)\" SET "OSARCH=x64"
IF NOT DEFINED OSARCH SET "OSARCH=x86"

:DoneArchCheck
REM SetACL location and move if present.
IF /I "%OSARCH%" equ "x86" IF NOT EXIST "%WinDir%\SetACL.exe" IF EXIST "%UTLoc%\setaclx86.exe" MOVE /Y "%UTLoc%\setaclx86.exe" "%WinDir%\SetACL.exe" >NUL 2>&1
IF /I "%OSARCH%" equ "x64" IF NOT EXIST "%WinDir%\SetACL.exe" IF EXIST "%UTLoc%\setaclx64.exe" MOVE /Y "%UTLoc%\setaclx64.exe" "%WinDir%\SetACL.exe" >NUL 2>&1

IF EXIST "%WinDir%\SetACL.exe" SET "SetACL=%WinDir%\SetACL.exe"

REM Query OS Installed. 
    SET "is810=0"
    SET "ChkVer="
    SET "ChkFlag="
    SET "After10="
    for /f "tokens=4-5 delims=. " %%i in ('ver') do set "ChkVer=%%i.%%j"
    IF "%ChkVer%"=="12.0" SET "ChkFlag=12" & SET "is810=1" & SET "WINVER=10" & SET "After10=1" & goto :Win12
    IF "%ChkVer%"=="11.0" SET "ChkFlag=11" & SET "is810=1" & SET "WINVER=10" & SET "After10=1" & goto :Win11
    IF "%ChkVer%"=="10.0" SET "ChkFlag=10" & SET "is810=1" & SET "WINVER=10" & goto :Win10
    IF "%ChkVer%"=="6.3" SET "ChkFlag=8_1" & SET "is810=1" & SET "WINVER=8_1" & goto :Win8_1
    IF "%ChkVer%"=="6.2" SET "ChkFlag=8" & SET "is810=1" & SET "WINVER=8" & goto :Win8
    IF "%ChkVer%"=="6.1" SET "ChkFlag=7" & SET "is810=0" & SET "WINVER=7" & goto :Win7
    IF "%ChkVer%"=="6.0" SET "ChkFlag=VISTA" & SET "is810=0" & SET "WINVER=VISTA" & goto :Vista

REM Incase something not picked up correctly such as Insider Previews.
%RQR1% | findstr /I "\<WINDOWS\>" | findstr /I "\<12\>" >nul && %INL% SET "ChkFlag=12" & SET "WINVER=10" & SET "is810=1" & SET "After10=1" & goto :Win12
%RQR1% | findstr /I "\<WINDOWS\>" | findstr /I "\<11\>" >nul && %INL% SET "ChkFlag=11" & SET "WINVER=10" & SET "is810=1" & SET "After10=1" & goto :Win11
%RQR1% | findstr /I "\<WINDOWS\>" | findstr /I "\<10\>" >nul && %INL% SET "ChkFlag=10" & SET "WINVER=10" & SET "is810=1" & goto :Win10
%RQR1% | findstr /I "\<WINDOWS\>" | findstr /I "\<8.1\>" >nul && %INL% SET "WINVER=8_1" & SET "is810=1" & goto :Win8_1
%RQR1% | findstr /I "\<WINDOWS\>" | findstr /I "\<8\>" >nul && %INL% SET "WINVER=8" & SET "is810=1" & goto :Win8
%RQR1% | findstr /I "\<WINDOWS\>" | findstr /I "\<7\>" >nul && %INL% SET "WINVER=7" & goto :Win7
%RQR1% | findstr /I "\<WINDOWS\>" | findstr /I "\<VISTA\>" >nul && %INL% SET "WINVER=VISTA" & goto :Vista
%RQR1% | findstr /I "\<WINDOWS\>" | findstr /I "\<PREVIEW\>" >nul && %INL% SET "WINVER=10" & SET "is810=1" & goto :Prev

REM If no supported version then finish the script.
GOTO :FinishedTweaks

REM ======================================================================================================
REM ======================================[ You can edit below ]==========================================
REM ======================================================================================================

:Win10

REM Don't change the filenames.
SET "MRPHKCU=W10CU.reg"
SET "MRPHKLM=W10LM.reg"
CALL :DoRegStuff


GOTO :FinishedTweaks

REM ======================================================================================================

:Win11

REM Place any Windows 1x tweaks here. Where specific windows version items have checked and made sections.

REM Don't change the filenames.
SET "MRPHKCU=W11CU.reg"
SET "MRPHKLM=W11LM.reg"
CALL :DoRegStuff

rem Example tweak...
Rem Change Scrollbar Width Windows 11 -- After10 variable is so that you can exclude below win11 for that section.
    IF DEFINED After10 (
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "BorderWidth" /t REG_SZ /d "-15" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "CaptionFont" /t REG_BINARY /d "f4ffffff0000000000000000000000009001000000000001000005005300650067006f006500200055004900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "CaptionHeight" /t REG_SZ /d "-330" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "CaptionWidth" /t REG_SZ /d "-330" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "IconFont" /t REG_BINARY /d "f4ffffff0000000000000000000000009001000000000001000005005300650067006f006500200055004900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "IconTitleWrap" /t REG_SZ /d "1" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "MenuFont" /t REG_BINARY /d "f4ffffff0000000000000000000000009001000000000001000005005300650067006f006500200055004900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "MenuHeight" /t REG_SZ /d "-285" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "MenuWidth" /t REG_SZ /d "-285" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "MessageFont" /t REG_BINARY /d "f4ffffff0000000000000000000000009001000000000001000005005300650067006f006500200055004900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "ScrollHeight" /t REG_SZ /d "-320" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "ScrollWidth" /t REG_SZ /d "-320" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "Shell Icon Size" /t REG_SZ /d "32" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "SmCaptionFont" /t REG_BINARY /d "f4ffffff0000000000000000000000009001000000000001000005005300650067006f006500200055004900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "SmCaptionHeight" /t REG_SZ /d "-330" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "SmCaptionWidth" /t REG_SZ /d "-330" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "StatusFont" /t REG_BINARY /d "f4ffffff0000000000000000000000009001000000000001000005005300650067006f006500200055004900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "PaddedBorderWidth" /t REG_SZ /d "-60" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "AppliedDPI" /t REG_DWORD /d "96" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "IconSpacing" /t REG_SZ /d "-1650" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "IconVerticalSpacing" /t REG_SZ /d "-1200" /f >nul 2>&1
        REG add "HKCU\Control Panel\Desktop\WindowMetrics" /v "MinAnimate" /t REG_SZ /d "1" /f >nul 2>&1
    )

GOTO :FinishedTweaks

REM ======================================================================================================

:Win12
REM Place any Windows 1x tweaks here. Where specific windows version items have checked and made sections.

REM Don't change the filenames.
SET "MRPHKCU=W12CU.reg"
SET "MRPHKLM=W12LM.reg"
CALL :DoRegStuff

GOTO :FinishedTweaks

REM ======================================================================================================

:Win8_1
REM Place any Windows 8.1 tweaks here.

SET "MRPHKCU=W81CU.reg"
SET "MRPHKLM=W81LM.reg"
CALL :DoRegStuff

GOTO :FinishedTweaks
REM ======================================================================================================

:Win8
REM Place any Windows 8.0 tweaks here - some can be used for 8.1

SET "MRPHKCU=W8CU.reg"
SET "MRPHKLM=W8LM.reg"
CALL :DoRegStuff

GOTO :FinishedTweaks
REM ======================================================================================================

:Win7
REM Place any Windows 7 tweaks here.

SET "MRPHKCU=W7CU.reg"
SET "MRPHKLM=W7LM.reg"
CALL :DoRegStuff

GOTO :FinishedTweaks
REM ======================================================================================================

:Vista
REM Place any Windows Vista tweaks here.

SET "MRPHKCU=WVCU.reg"
SET "MRPHKLM=WVLM.reg"
CALL :DoRegStuff

GOTO :FinishedTweaks
REM ======================================================================================================

:Prev
REM Place any Windows 1x Preview specific tweaks here.
REM Change the filename from W10PCU/W10PLM .reg to W11 or W12 if you specific versions are required, or just group as w10 ones.
SET "MRPHKCU=W10PCU.reg"
SET "MRPHKLM=W10PLM.reg"
CALL :DoRegStuff

GOTO :FinishedTweaks
REM ======================================================================================================

REM ======================================================================================================
REM ======================================[ Subroutines Below ] ==========================================
REM ======================================================================================================
:DoRegStuff
    IF EXIST "%UTLoc%\%MRPHKCU%" (
    regedit /s "%UTLoc%\%MRPHKCU%" >NUL 2>&1
    DEL /F /Q "%UTLoc%\%MRPHKCU%" >NUL 2>&1
    )

    IF EXIST "%UTLoc%\%MRPHKLM%" (
    regedit /s "%UTLoc%\%MRPHKLM%" >NUL 2>&1
    DEL /F /Q "%UTLoc%\%MRPHKLM%" >NUL 2>&1
    )

EXIT /b

REM ======================================================================================================
REM Example call :RegOwn "HKEY_LOCAL_MACHINE\SYSTEM\blah blah"
:RegOwn
    IF NOT EXIST "%SetACL%" GOTO :EOF
    IF /I "%~1"=="" goto :EOF

    REM Changing Registry Key Ownership.
    "%SetACL%" -on "%~1" -ot reg -actn setowner -ownr "n:S-1-5-32-544;s:y" -rec yes >NUL 2>&1
 
    REM Setting Registry Key Permission.
    "%SetACL%" -on "%~1" -ot reg -actn ace -ace "n:S-1-5-32-544;s:y;p:full" -rec yes >NUL 2>&1

EXIT /b
REM ======================================================================================================

:FinishedTweaks
IF /I "%errorlevel%" NEQ "0" SET "ErrCode=%errorlevel%"
IF EXIST "%UTLoc%\" RD /S /Q "%UTLoc%" >NUL 2>&1
IF EXIST "%UTLoc%\*.reg" DEL /F /Q "%UTLoc%\*.reg" >NUL 2>&1
IF EXIST "%WinDir%\SetACL.exe" DEL /Q /F "%WinDir%\SetACL.exe" >NUL 2>&1
IF EXIST "%SetACL%" DEL /Q /F "%SetACL%" >NUL 2>&1
DEL /F /Q "%0%" >NUL 2>&1