@echo off
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 Win10 , Win7 etc.
REM -
REM 	This Add-On was created to help you add your own tweaks easier.
REM -
REM 	To use registry (.reg) files use the following filenames:
REM -
REM 	For Windows 10:
REM 	W10CU.reg for Current User entries.
REM 	W10LM.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 10 Preview specific:
REM 	W10PCU.reg for Current User entries.
REM 	W10PLM.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/10 .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, REM above SET UTLoc= and deREM 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.

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

: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="
	for /f "tokens=4-5 delims=. " %%i in ('ver') do set "ChkVer=%%i.%%j"
	IF "%ChkVer%"=="10.2" SET "ChkFlag=10" & SET "is810=1" & SET "WINVER=10" & goto :Win10
	IF "%ChkVer%"=="10.1" SET "ChkFlag=10" & SET "is810=1" & SET "WINVER=10" & goto :Win10
	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 "\<10\>" >nul && %INL% 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 Place any Windows 10 tweaks here.

SET "MRPHKCU=W10CU.reg"
SET "MRPHKLM=W10LM.reg"
CALL :DoRegStuff

REM Examples of REG ADD.  Remove the REM before the reg add line if you wish to use.
REM reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "OneDrive" /f >NUL 2>&1
REM reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "OneDriveSetup" /f >NUL 2>&1

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 10 Preview specific tweaks here.
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
	DEL /F /Q "%UTLoc%\%MRPHKCU%" >nul
	)

	IF EXIST "%UTLoc%\%MRPHKLM%" (
	regedit /s "%UTLoc%\%MRPHKLM%" >nul
	DEL /F /Q "%UTLoc%\%MRPHKLM%" >nul
	)
GOTO :EOF
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

GOTO :EOF
REM ======================================================================================================

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