@echo off
call :setting
::=============================================================================================================================================================
::EX:
::Set keyword=21.00.0.4
::call :uninstall
:driver

Set keyword=3.12344.0.190
call :uninstall
Set keyword=10.29.00.4479
call :uninstall






::=============================================================================================================================================================
::EX:
::Set UWPkeyword=RealtekAudioControl
::call :uninstallUWP

:UWP
Set UWPkeyword=
call :uninstallUWP

Set UWPkeyword=
call :uninstallUWP

Set UWPkeyword=
call :uninstallUWP

Set UWPkeyword=
call :uninstallUWP


::Please do not change below==================================================================================================================================
goto :end


:uninstall
if %keyword%NOkeyword==NOkeyword goto :skip_u

find /i "%keyword%" C:\Windows\INF\oem*.inf

if %errorlevel% GEQ 1 echo %date% %time% Driver keyword [%keyword%] cannot be found under C:\Windows\INF\oem*.inf on the unit. Skip >>%errorlog%
if %errorlevel% GEQ 1 goto :skip_u

echo %date% %time% Pnputil to uninstall driver related with [%keyword%] >>%errorlog%

powershell -command "ls -r -path C:\windows\inf -filter oem*.inf | ?{$_ | select-string -pattern '%keyword%' -simplematch } | select -exp name | foreach($_) { pnputil /delete-driver $_ /force /uninstall >UnInstall_$_.txt  }"

:skip_u
 
Set keyword=

exit /b



:uninstallUWP
if %UWPkeyword%NOkeyword==NOkeyword goto :skip_u_uwp

find /i "%UWPkeyword%" %UWPAPPBefore%

if %errorlevel% GEQ 1 echo %date% %time% UWP APP keyword [%UWPkeyword%] cannot be found in UWPAPP list. Skip >>%errorlog%
if %errorlevel% GEQ 1 goto :skip_u_uwp

echo %date% %time% Uninstall UWPAPP related with [%UWPkeyword%] >>%errorlog%

powershell -command "get-appxpackage *%UWPkeyword%* | remove-appxpackage -AllUsers >>UnInstall_UWP_%UWPkeyword%.txt"

:skip_u_uwp

Set UWPkeyword=

exit /b


:setting
title %~n0
echo ****************************************************************************************************************
echo [ UnInstall Driver by pnputil ]
echo Set the keyword for the driver you want to uninstall. 
echo Driver keyword can be driver version, cat file name, etc. which is specific for the driver.
echo Driver version showing for the pnputil /e output may not the same as oem.inf. (EX: 19.51.19.1 vs 19.51.19.01).
echo Please use version showing in oem.inf as the keyword (EX: 19.51.19.01). 
echo.
echo ****************************************************************************************************************
echo [ UnInstall UWPAPP by powershell command ] 
echo Please set the keyword for the UWP APP you want to uninstall. (ex: IntelGraphicsControlPanel) 
echo.
echo ****************************************************************************************************************
echo [ Edit script for auto-run to uninstall more than 1 version driver/UWPAPP ]
echo Right click to edit this bat file to Set Driver keyword and UWP keyword.
echo.
echo.


cd /d "%~dp0"
Set log="%~dp0"
Set logoemBefore=List_oemInf_Before.txt
Set logoemAfter=List_oemInf_After.txt
Set UWPAPPBefore=List_UWPAPP_Before.txt
Set UWPAPPAfter=List_UWPAPP_After.txt
Set Errorlog=UnInstall_log.txt


pnputil /e >%logoemBefore%
Dism /online /get-ProvisionedAppxpackages >%UWPAPPBefore%
echo %Date% %Time% Start log ==================================================================================================================================================>%Errorlog%
echo Select within 10 sec
echo 1 Key-in (driver keyword and UWP keyword)
echo 2 Set in script directly (Default)

echo.
if %errorlevel% EQU 1 Set /p "keyin=Enter Keyword for driver-uninstall (Press Enter to skip):"
echo.
if %errorlevel% EQU 1 Set /p "keyin2=Enter Keyword for UWP APP-uninstall(Press Enter to skip):"
if %errorlevel% EQU 2 goto :Auto


Set keyword=%keyin%
call :uninstall

Set UWPkeyword=%keyin2%
call :uninstallUWP

:Auto

exit /b

:end

pnputil /e >%logoemAfter%
Dism /online /get-provisionedappxpackages >%UWPAPPAfter%

echo %date% %time%============================================================================================================================================================>>%Errorlog%
echo %date% %time% Compare OEM.inf list>>%Errorlog% 
echo %date% %time% %logoembefore% >>%Errorlog% & echo %date% %time% %logoemafter% >>%Errorlog% & echo. >>%Errorlog%
powershell -command "Compare-Object -ReferenceObject $(Get-content %logoembefore%) -DifferenceObject $(get-content %logoemafter%) -passThru |out-file %Errorlog% -append -encoding default"
echo %date% %time% ===========================================================================================================================================================>>%Errorlog%
echo %date% %time% Compare UWPAPP list>>%Errorlog%
echo %date% %time% %UWPAPPBefore% >>%Errorlog% & echo %date% %time% %UWPAPPAfter% >>%Errorlog% & echo. >>%Errorlog%
powershell -command "Compare-Object -ReferenceObject $(Get-content %UWPAPPBefore%) -DifferenceObject $(get-content %UWPAPPAfter%) -passThru |out-file %Errorlog% -append -encoding default"
echo %date% %time% END========================================================================================================================================================>>%Errorlog%

start %Errorlog%

REM Release Note
REM 3rd release by Myra Chung 2020/04/06
REM Fix: Use -simplematch to fix issue: mis-un-install other driver with keyword not found in the oemx.inf

REM 2nd release by Myra Chung 2019/12/06
REM Fix: Path/folder with space will cause script error. 

REM 1st release by Myra Chung 2019/8/28



