New
#1
Windows 10 / 11 post WU installation cleanup script
Hey, folks,
Thanks to a lot of wonderful folks over at 11F, 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) | Page 2 | Windows 11 Forum
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) | Page 3 | Windows 11 Forum
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) | Page 3 | Windows 11 Forum
Thus far I've just been right clicking the file and selectingRun 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:
windows - How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required? - Stack Overflow
I've also added apause
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:
Code::: 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, I've attached it for you.
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!