Windows 10 / 11 post WU installation cleanup script


  1. Posts : 2,667
    Windows 11 21H2 (22000.593)
       #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 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:

    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 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:

    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!
    Windows 10 / 11 post WU installation cleanup script Attached Files
      My Computers


  2. Posts : 15,494
    Windows10
       #2

    johngalt said:
    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 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:

    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 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:

    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!
    I am not sure why people would need a batch file when you can simply run diskcleanup which only takes a few clicks.

    In fact Windows 11 is far superior to Windows 10 as it pretty much cleans itself up after updates. I check now and then but never much to clear up.

    The update philosophy of Windows 11 is moving from the traditional OS overwrite method to the enablement method which helps i.e. you do not end up with 10+GB to clean up.

    Also, if you configure Storage Sense properly, the cleanups are mostly done in the background.
      My Computer


  3. Posts : 2,667
    Windows 11 21H2 (22000.593)
    Thread Starter
       #3

    Over at Windows Eleven Forum (11F), user rpo has posted batch code that uses PS to create a GUI so that the end user can pick which items to run.

    Windows 10 / 11 post WU installation cleanup script | Windows 11 Forum

    Code:
    <# : @Powershell -noprofile -exec bypass -WindowStyle Hidden "iex((Get-Content('%~f0') -Raw))"&exit/b
    #>
    #
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null # Import the Assemblies
    #
    # Form Objects
    $Form = New-Object System.Windows.Forms.Form
    $Form.KeyPreview = $True
    $Form.TopMost = $True
    $Form.FormBorderStyle = "none"
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$OKButton.PerformClick()}})         # Enter = click OK button
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})    # Escape = click on Cancel button
    $Form.BackColor = "SteelBlue"
    $Form.ForeColor = "WhiteSmoke"
    $Form.ClientSize = New-Object System.Drawing.Size(460,400)
    $Form.StartPosition = "CenterScreen"
    #
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(38,350)
    $CancelButton.Size = New-Object System.Drawing.Size(140,25)
    $CancelButton.Text = "Exit"
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $Form.Controls.Add($CancelButton)
            
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(260,350)
    $OKButton.Size = New-Object System.Drawing.Size(140,25)
    $OKButton.Text = "OK"
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $Form.Controls.Add($OKButton)
    
    $label1 = New-Object System.Windows.Forms.Label
    $label1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
    $label1.Location = New-Object System.Drawing.Point(30,50)
    $label1.Size = New-Object System.Drawing.Size(400,23)
    $label1.Text = "Windows 10 / 11 post WU installation cleanup scripts"
    $Form.Controls.Add($label1)
    
    $checkBox1 = New-Object System.Windows.Forms.CheckBox
    $checkBox1.Location = New-Object System.Drawing.Point(20,100)
    $checkBox1.Size = New-Object System.Drawing.Size(500,60)
    $text1="rundll32.exe pnpclean.dll,RunDLL_PnpClean /drivers/maxclean"
    $checkBox1.Text = "rundll32.exe pnpclean.dll,RunDLL_PnpClean /drivers/maxclean"
    $Form.Controls.Add($checkBox1)
    
    $checkBox2 = New-Object System.Windows.Forms.CheckBox
    $checkBox2.Location = New-Object System.Drawing.Point(20,140)
    $checkBox2.Size = New-Object System.Drawing.Size(500,60)
    $checkBox2.Text = "dism /Online /Cleanup-Image /CheckHealth"
    $Form.Controls.Add($checkBox2)
    
    $checkBox3 = New-Object System.Windows.Forms.CheckBox
    $checkBox3.Location = New-Object System.Drawing.Point(20,180)
    $checkBox3.Size = New-Object System.Drawing.Size(500,60)
    $checkBox3.Text = "sfc /scannow"
    $Form.Controls.Add($checkBox3)
    
    $checkBox4 = New-Object System.Windows.Forms.CheckBox
    $checkBox4.Location = New-Object System.Drawing.Point(20,220)
    $checkBox4.Size = New-Object System.Drawing.Size(500,60)
    $checkBox4.Text = "dism /Online /Cleanup-Image /RestoreHealth"
    $Form.Controls.Add($checkBox4)
    
    $checkBox5 = New-Object System.Windows.Forms.CheckBox
    $checkBox5.Location = New-Object System.Drawing.Point(20,260)
    $checkBox5.Size = New-Object System.Drawing.Size(500,60)
    $checkBox5.Text = "dism /online /Cleanup-Image /Startcomponentcleanup /resetbase"
    $Form.Controls.Add($checkBox5)
    
    if ($Form.ShowDialog() -eq "Cancel"){exit}
    
    if ($checkbox1.checked){Start-Process cmd -ArgumentList '/c rundll32.exe "pnpclean.dll,RunDLL_PnpClean /drivers/maxclean"&pause' -wait -verb RunAs}
    if ($checkbox2.checked){Start-Process cmd -ArgumentList '/c dism /Online /Cleanup-Image /CheckHealth&pause' -wait -verb RunAs}
    if ($checkbox3.checked){Start-Process cmd -ArgumentList '/c sfc /verifyonly&pause' -wait -verb RunAs}
    if ($checkbox4.checked){Start-Process cmd -ArgumentList '/c dism /Online /Cleanup-Image /RestoreHealth&pause' -wait -verb RunAs}
    if ($checkbox5.checked){Start-Process cmd -ArgumentList '/c dism /online /Cleanup-Image /Startcomponentcleanup /resetbase&pause' -wait -verb RunAs}
    Start-Process cmd -ArgumentList '/c echo End of process&pause' -wait
    I think this is a fantastic version, and I'm working on some edits to make it a bit more versatile, including options to run all as well as pick either sfc with or without f / r options.
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 13:15.
Find Us




Windows 10 Forums