@echo off

REM This will only work on Windows 10 builds greater than 17665.

TITLE Windows 10: Re-Enable 'Enhanced' Clipboard History.
COLOR 1F
echo.

cd %systemroot%\system32
call :IsAdmin
CLS
TITLE Windows 10: Re-Enable 'Enhanced' Clipboard History.
cls
color 1f
echo.
ver | findstr /i " 10\.0\." > nul
IF "%ERRORLEVEL%" EQU "0" goto :Win10
echo This script only works on Windows 10 Kernels.
echo.
pause
exit

:Win10
echo.
echo Simple script to Re-Enable the 'Enhanced' Clipboard History feature.
echo.

SET "ErrorsFound="
SET "RS5PlusX="
set "CBHServiceExists="

for /f "tokens=4-5 delims=. " %%i in ('ver') do set "TestXWinVer1=%%i"
for /f "tokens=6 delims=[]. " %%O in ('ver') do set "OSBuildVer=%%O"

IF NOT DEFINED OSBuildVer SET "ErrorsFound=1" & GOTO :NoBVerDet0
IF NOT DEFINED TestXWinVer1 SET "ErrorsFound=2" & GOTO :NoBVerDet0
IF /I "%TestXWinVer1%" NEQ "10" SET "ErrorsFound=3" & GOTO :NoBVerDet0

IF /I "%OSBuildVer%" GTR "17665" SET "RS5PlusX=Yes"
IF NOT DEFINED RS5PlusX SET "ErrorsFound=4" & GOTO :NoBVerDet0

for /F "tokens=3" %%A in ('REG query "HKLM\System\CurrentControlSet\Services\cbdhsvc" /v "Start" 2^>nul') do set "CBHServiceExists=%%A"

IF NOT DEFINED CBHServiceExists SET "ErrorsFound=5"
IF DEFINED ErrorsFound GOTO :NoBVerDet0

reg add "HKLM\System\CurrentControlSet\Services\cbdhsvc" /v "Start" /t REG_DWORD /d "2" /f >nul 2>&1
Reg delete "HKLM\Software\Microsoft\Clipboard" /v "EnableClipboardHistory" /f >nul 2>&1
reg delete "HKLM\Software\Policies\Microsoft\Windows\System" /v "AllowCrossDeviceClipboard" /f >nul 2>&1
reg delete "HKLM\Software\Policies\Microsoft\Windows\System" /v "AllowClipboardHistory" /f >nul 2>&1
Reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\System" /f >nul 2>&1

echo.
echo Done. Note: Reboot is required to enable the Clipboard History Service.
GOTO :Finished

:NoBVerDet0
IF /I "%ErrorsFound%"=="1" echo Error: Cannot determine OS Build number.
IF /I "%ErrorsFound%"=="2" echo Error: Cannot determine Windows Kernel.
IF /I "%ErrorsFound%"=="3" echo Error: Not Windows 10.
IF /I "%ErrorsFound%"=="4" echo Error: Windows 10 Build below 17665.
IF /I "%ErrorsFound%"=="5" echo Error: Clipboard History Service not found.

echo.
echo Nothing changed as an error detected -- script aborting...

:Finished
pause
exit

:IsAdmin
Reg.exe query "HKU\S-1-5-19\Environment"
If Not %ERRORLEVEL% EQU 0 (
 Cls
 echo.
 Echo You must have administrator rights to continue...
 echo.
 Pause
 Exit
)
