Hey, folks,
Thanks to a lot of wonderful folks here, we've made mention in a previous Insider topic about cleanup steps that can be run after Windows Update performs a major version install.
Though these have been mentioned before by many, many people, it was this post by @IanMosley that started me on implementing this as an step to clean up both my main Windows installation and all the VMs I have for testing:
A few posts later, @geneo offered some good advice on how to streamline that set of commands in this post:
Which, eventually, led me to mentioning that I had put them all in a batch file to run after major WU installs:
Thus far I've just been right clicking the file and selecting
I found a good thread at Stack Overflow that has a few methods mentioned, including direct self-elevation, elevation via WSH, via VBScripting, and even via Powershell. I tested several ones, and found that this particular script works best and is no nonsense and has no fuss:
I've also added a
So, here is the code as I'm using it now:
You can copy the contents and save it to a file with a .BAT extension and it should work just fine, tested across Windows 10 and Windows 10 Insider Preview, plus Windows 11 (RTM, Release Preview, Beta, Dev and Canary current builds).
If you'd rather just download the batch file, here you go.
Download
Enjoy!
P.S. - if you have any suggestions on improving it / adding more functionality / expanding the scope of the cleanup, feel free to comment below!
Thanks to a lot of wonderful folks here, we've made mention in a previous Insider topic about cleanup steps that can be run after Windows Update performs a major version install.
Though these have been mentioned before by many, many people, it was this post by @IanMosley that started me on implementing this as an step to clean up both my main Windows installation and all the VMs I have for testing:
KB5029351 Windows 11 Cumulative Update Preview Build 22621.2215 (22H2)
UPDATE 9/12: https://www.elevenforum.com/t/kb5030219-windows-11-cumulative-update-build-22621-2283-22h2.17886/ Microsoft Support: August 22, 2023 - KB5029351 (OS Build 22621.2215) Preview For information about Windows update terminology, see the article about the types of Windows updates and...
www.elevenforum.com
A few posts later, @geneo offered some good advice on how to streamline that set of commands in this post:
KB5029351 Windows 11 Cumulative Update Preview Build 22621.2215 (22H2)
UPDATE 9/12: https://www.elevenforum.com/t/kb5030219-windows-11-cumulative-update-build-22621-2283-22h2.17886/ Microsoft Support: August 22, 2023 - KB5029351 (OS Build 22621.2215) Preview For information about Windows update terminology, see the article about the types of Windows updates and...
www.elevenforum.com
Which, eventually, led me to mentioning that I had put them all in a batch file to run after major WU installs:
KB5029351 Windows 11 Cumulative Update Preview Build 22621.2215 (22H2)
UPDATE 9/12: https://www.elevenforum.com/t/kb5030219-windows-11-cumulative-update-build-22621-2283-22h2.17886/ Microsoft Support: August 22, 2023 - KB5029351 (OS Build 22621.2215) Preview For information about Windows update terminology, see the article about the types of Windows updates and...
www.elevenforum.com
Thus far I've just been right clicking the file and selecting
Run as Administrator
to run it - not all that hard. But, today, I went in search of a self-elevation script for batch files so I can just double click the file and let it do its thing.I found a good thread at Stack Overflow that has a few methods mentioned, including direct self-elevation, elevation via WSH, via VBScripting, and even via Powershell. I tested several ones, and found that this particular script works best and is no nonsense and has no fuss:
How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?
I want my batch file to only run elevated. If not elevated, provide an option for the user to relaunch batch as elevated. I'm writing a batch file to set a system variable, copy two files to a Pro...
stackoverflow.com
I've also added a
pause
command at the end so that the window remains on screen after completing so you can see the results, and a simple keypress will close it out.So, here is the code as I'm using it now:
Batch:
:: Elevation script from StackOverflow
:: https://stackoverflow.com/a/28467343
:: Begin Elevation script
@ECHO OFF
setlocal EnableDelayedExpansion
::net file to test privileges, 1>NUL redirects output, 2>NUL redirects errors
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto START ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' ( goto START )
set "batchPath=%~f0"
set "batchArgs=ELEV"
::Add quotes to the batch path, if needed
set "script=%0"
set script=%script:"=%
IF '%0'=='!script!' ( GOTO PathQuotesDone )
set "batchPath=""%batchPath%"""
:PathQuotesDone
::Add quotes to the arguments, if needed.
:ArgLoop
IF '%1'=='' ( GOTO EndArgLoop ) else ( GOTO AddArg )
:AddArg
set "arg=%1"
set arg=%arg:"=%
IF '%1'=='!arg!' ( GOTO NoQuotes )
set "batchArgs=%batchArgs% "%1""
GOTO QuotesDone
:NoQuotes
set "batchArgs=%batchArgs% %1"
:QuotesDone
shift
GOTO ArgLoop
:EndArgLoop
::Create and run the vb script to elevate the batch file
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "cmd", "/c ""!batchPath! !batchArgs!""", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:START
::Remove the elevation tag and set the correct working directory
IF '%1'=='ELEV' ( shift /1 )
cd /d %~dp0
::Do your adminy thing here...
:: End Elevation Script
:: Begin cleanup script
start /B /wait rundll32.exe pnpclean.dll,RunDLL_PnpClean /drivers/maxclean
start /B /wait dism /Online /Cleanup-Image /CheckHealth
start /B /wait sfc /scannow
start /B /wait dism /Online /Cleanup-Image /RestoreHealth
start /B /wait dism /online /Cleanup-Image /Startcomponentcleanup /resetbase
pause
You can copy the contents and save it to a file with a .BAT extension and it should work just fine, tested across Windows 10 and Windows 10 Insider Preview, plus Windows 11 (RTM, Release Preview, Beta, Dev and Canary current builds).
If you'd rather just download the batch file, here you go.
Download
Enjoy!
P.S. - if you have any suggestions on improving it / adding more functionality / expanding the scope of the cleanup, feel free to comment below!
- Windows Build/Version
- Windows 10 / Windows 11
Attachments
Last edited by a moderator: