REM =================
REM Console Settings:
REM =================
Title WIM Feature Uninstaller - By Elektro
Mode
Con Cols=150 Lines=50
CHCP 1252 1
>NUL & REM Windows-1252, Spanish-Latin.
REM =====
REM Info:
REM =====
Echo -----------------------------------------------------------------------------
Echo This script mounts a Windows Image
^(WIM
^) and then disables Windows features
Echo -----------------------------------------------------------------------------
REM ====================
REM User defined values:
REM ====================
REM This value indicates whether the Windows Image should be mounted.
REM True = 'DISM' will try to mount the 'WIM' image.
REM False = 'DISM' will not try to mount the WIM image.
REM Set this value to 'FALSE' if the 'WIM' image is already mounted in the specified mount directory,
REM Otherwise, keep it as 'TRUE'.
REM This value indicates whether the Windows Image should be unmounted at the end.
REM True = 'DISM' will try to unmount the 'WIM' image.
REM False = 'DISM' will not try to unmount the WIM image.
REM Set this value to 'FALSE' if you want to keep the image mounted to make other changes,
REM Otherwise, keep it as 'TRUE'.
REM This value indicates whether the Windows Image should be rebuilt at the end.
REM True = 'ImageX' will try to rebuild the 'WIM' image.
REM False = 'ImageX' will keep the 'WIM' image as is.
REM Set this value to 'FALSE' if you want to keep the image with normal size to make other changes,
REM Otherwise, keep it as 'TRUE'.
REM This value indicates the 'WIM' image to be mounted.
Set "WindowsImage=
%UserProfile%\Desktop\win 8 .1\win\sources\install.wim"
REM This value indicates the Image Index of the 'WIM' image.
REM This value indicates the directory where to mount the 'WIM' image.
Set "MountDir=
%HomeDrive%\WinMount"
REM This value indicates the textfile that contains the names of the features to disable.
Set "FeatureList=
%CD%\Features.txt"
REM This value indicates the ubication of 'ImageX.exe' file.
Set "ImageX=
%CD%\Tools\Dism\ImageX.exe"
REM This value indicates the ubication of a custom 'DISM.exe' file if needed.
REM Default ubication: "%SystemRoot%\System32\Dism.exe"
Set "Dism=
%CD%\Tools\Dism\Dism.exe"
REM This value indicates the ubication of the logfile that will record 'DISM' errors.
Set "DismLogfile=
%CD%\
%~n0 DISM.log"
REM This value indicates the logging-level of the 'DISM' process.
REM 1 = Errors only
REM 2 = Errors and warnings
REM 3 = Errors, warnings, and informational
REM 4 = All of the information listed previously, plus debug output
REM This value indicates the ubication of the logfile that will record Main information.
Set "Logfile=
%CD%\
%~n0.log"
REM =====
REM Main:
REM =====
:: Call Methods.
CLS
CLS
REM ========
REM Methods:
REM ========
:CreateLogs
:: Create the 'DISM' logfile with 'ANSI' encoding,
:: this is necessary to record a non-default 'DISM' loglevel such as '1' or '2'.
:: Create the Main logfile and record starting info on it.
(
Echo WIM Feature Uninstaller
Echo ===========================
Echo %DATE% ^| %TIME:~0,-3% Echo /\/\/\/\/\/\/\/\/\/\/\/\/\/
Echo [i] Mount Image?...:
%MountImage% Echo [i] Unmount Image?.:
%UnmountImage% Echo [i] Windows Image..:
%WindowsImage% Echo [i] Image Index....:
%ImageIndex% Echo [i] Mount Directory:
%MountDir% Echo [i] ImageX Path....:
%ImageX% Echo [i] Dism Path......:
%Dism% Echo [i] Dism Log File..:
%DismLogfile% Echo [i] Dism Log Level.:
%DismLogLevel% Echo [i] Feature List...:
%FeatureList% Echo [i] Log File.......:
%Logfile% Echo ===========================
)>"%Logfile%"
:: Display starting log info.
Type "%Logfile%"
:: Continue writting log.
(
)>>"%LogFile%"
:EndLog
(
Echo [i] WIM Image Size Before.........:
%WIMSizeBefore% Echo [i] WIM Image Size After..........:
%WIMSizeAfter% Echo [i] Features Successfully Disabled:
%FeaturesRemovedCount% Packages.
Echo [i] Features Failed To Disable....:
%FeaturesFailedCount% Packages.
Echo [i] Features
Not Found............:
%FeaturesNotFoundCount% Packages.
Echo [i] Elapsed Time..................:
%ElapsedTime% )>>"%LogFile%"
:: Display ending information.
CLS
Type "%LogFile%"
:CreateMountDir
:: Create the mount directory.
(
)
) || (
Echo [x] Error trying to create the directory "
%MountDir%" to mount the Windows Image.
Echo [i] Ensure that you have Admin rights to create the directory.
Echo [i] Ensure that the directory name does
not contains illegal characters.
)
:Mount
:: Mount the Windows Image into the directory.
Echo [+] Mounting WIM Image...
If /I "
%MountImage%"
EQU "True"
(
"%Dism%" /Mount-Image /ImageFile:"%WindowsImage%" /Index:"%ImageIndex%" /MountDir:"%MountDir%" /English /LogPath:"%DismLogfile%" /LogLevel:"%DismLogLevel%" || (
Echo [x] Error mounting the Image Index "
%ImageIndex%"
in "
%MountDir%"
Echo [i] Ensure that you have Admin rights to write inside the directory.
Echo [i] Ensure that the directory is
not already mounted.
Echo [i] Ensure that the directory is fully empty.
REM Try to unmount the failed image and discard changes.
If /I "
%UnmountImage%"
EQU "True"
( Echo [+] Unmounting and discarding changes...
"%Dism%" /Unmount-WIM /Discard /MountDir:"%MountDir%"
)
)
)
:CleanUp
Echo [+] CleaningUp WIM Image...
"%Dism%" /CleanUp-Image /StartComponentCleanUp /Image:"%MountDir%" /English /LogPath:"%DismLogfile%" /LogLevel:"%DismLogLevel%" /Quiet
:Unmount
:: Unmount the Windows Image commiting changes.
Echo [+] Unmounting WIM Image...
If /I "
%UnmountImage%"
EQU "True"
( "%Dism%" /Unmount-WIM /Commit /MountDir:"%MountDir%" /English /LogPath:"%DismLogfile%" /LogLevel:"%DismLogLevel%"
)
:Rebuild
Echo [+] Rebuilding WIM image...
("%ImageX%" /Compress Maximum /Export "%WindowsImage%" "*" "%TEMP%\Rebuilt Image.wim" /Check) && (
Del /Q /F "
%WindowsImage%" 1
>NUL Move /Y "
%TEMP%\Rebuilt Image.wim" "
%WindowsImage%" 1
>NUL ) || (
Echo [x] Error rebuilding Image "
%WindowsImage%" to "
%TEMP%\Rebuilt Image.wim".
)
:StartTimer
Set "StartingDate=
%Date%"
Set "StarttingTime=
%Time:~0,-3%"
:StopTimer
(
Echo Minutes = DateDiff
^("n", "
%StartingDate% %StarttingTime%", Now
^) )>"%TEMP%\%~n0 MinuteDiff.vbs"
For /F "Tokens=*"
%%#
In ( 'Cscript.exe /Nologo "%TEMP%\%~n0 MinuteDiff.vbs"'
)
:WriteConvertBytesScript
(
Echo Suffix = Array
^("Bytes", "KB", "MB", "GB", "TB"
^) Echo Function Convert
^(Bytes
^) Echo Convert = convert0
^(Bytes, 0
^) Echo Function Convert0
^(Bytes, Level
^) Echo Bytes = Round
^(Bytes / 1024, 2
^) Echo Convert0 = Convert0
^(Bytes, Level
^) Echo Convert0 = Bytes
^& " "
^& Suffix
^(Level
^) Echo WScript.
Echo Convert
^(WScript.Arguments
^(0
^)^) )>"%TEMP%\%~n0 Convert Bytes.vbs"
:GetWIMSizeBefore
Call :
WriteConvertBytesScript For %%#
in ("
%WindowsImage%"
) Do (Set "WIMSizeBefore=
%%~z#"
) For /F "Tokens=*"
%%#
In ( 'Cscript.exe /Nologo "%TEMP%\%~n0 Convert Bytes.vbs" "%WIMSizeBefore%"'
)
Del /Q "
%TEMP%\
%~n0 Convert Bytes.vbs"
:GetWIMSizeAfter
Call :
WriteConvertBytesScript For %%#
in ("
%WindowsImage%"
) Do (Set "WIMSizeAfter=
%%~z#"
) For /F "Tokens=*"
%%#
In ( 'Cscript.exe /Nologo "%TEMP%\%~n0 Convert Bytes.vbs" "%WIMSizeAfter%"'
)
Del /Q "
%TEMP%\
%~n0 Convert Bytes.vbs"
:ParseFeatures
Set /A "FeaturesRemovedCount=0"
Set /A "FeaturesFailedCount=0"
Set /A "FeaturesNotFoundCount=0"
:: Disable Features.
For /F "UseBackQ Tokens=* Delims="
%%#
In ("
%FeatureList%"
) Do (
Echo [+] Disabling Feature:
%%~#
"
%Dism%" /Disable-Feature /FeatureName:"
%%~#" /Image:"
%MountDir%" /English /LogPath:"
%DismLogfile%" /LogLevel:"
%DismLogLevel%" /Quiet 1
>NUL Call Set /A "ExitCode=
%%Errorlevel%%"
(Call Echo "
%%ExitCode%%"
| Findstr.exe "
^\"0\"$" 1
>NUL 2
>&1
) && ( Call Set /A "FeaturesRemovedCount+=1"
)
(Call Echo "
%%ExitCode%%"
| Findstr.exe "
^\"5\"$" 1
>NUL 2
>&1
) && ( Call Set /A "FeaturesFailedCount+=1"
Echo Failed to disable feature:
%%~#
>>"
%LogFile%"
)
(Call Echo "
%%ExitCode%%"
| Find.exe "-2146498548" 1
>NUL 2
>&1
) && ( Call Set /A "FeaturesNotFoundCount+=1"
Echo Feature is
not found.....:
%%~#
>>"
%LogFile%"
)
)
REM ===============
REM Error Controls:
REM ===============
:CheckErrors
:: WIM File Error-Check.
Echo [x] Windows Image
not found at: "
%WindowsImage%"
| MORE
)
:: FeatureList File Error-Check.
Echo [x] FeatureList
not found at: "
%FeatureList%"
| MORE
)
:: 'DISM.exe' File Error-Check.
Echo [x] 'DISM' process
not found at: "
%DISM%"
| MORE
)
:: 'ImageX.exe' File Error-Check.
Echo [x] 'ImageX' process
not found at: "
%ImageX%"
| MORE
)
:: 'MountImage' Value check.
If /I "
%MountImage%"
NEQ "True"
If /I "
%MountImage%"
NEQ "False"
( Echo [x] Error parsing parameter 'MountImage',
Echo value '
%MountImage%' is
not a Boolean value.
)
:: 'UnmountImage' Value check.
If /I "
%UnmountImage%"
NEQ "True"
If /I "
%UnmountImage%"
NEQ "False"
( Echo [x] Error parsing parameter 'UnmountImage',
Echo value '
%UnmountImage%' is
not a Boolean value.
)
:: 'Rebuild' Value check.
If /I "
%Rebuild%"
NEQ "True"
If /I "
%Rebuild%"
NEQ "False"
( Echo [x] Error parsing parameter 'Rebuild',
Echo value '
%Rebuild%' is
not a Boolean value.
)
:: 'DismLogLevel' Value check.
Echo "
%DismLogLevel%"
| Findstr.exe "
^\"[
^1-4]\"$" 1
>NUL 2
>&1
|| ( Echo [x] Error parsing parameter 'DismLogLevel',
Echo value '
%DismLogLevel%' is
not in range '1-4'.
)