Start-WindowsCleanup

Discussion in 'Scripting' started by GodHand, Sep 18, 2019.

  1. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    #1 GodHand, Sep 18, 2019
    Last edited: Dec 7, 2020
    Updated 12/06/2020
    - A new registry key gets added that contains both the function's last run time as well as the StateFlags integer value. Each time the function is run, it checks this registry key and ensures none of the previous StateFlags' integer values are still present. This will ensure any set StateFlags integer values are removed if the clean-up process is exited or closed before the Windows Disk Clean-up utility has completed.
    - Included the removal of additional histories and recent documents.
    - Additional primary function and helper function updates and improvements.

    Updated 08/26/2020
    - The OS is now checked before removing shadow copies (restore points) to make sure it is not a version of Windows Server.
    - Added additional default and Microsoft Edge removal paths.
    - Added the clearing of the App Compat Cache and CD burning registry keys.

    I cannot post the full code of the module any longer due to exceeding the character limit for a single post, but I created a GitHub Gist for it where you can view it and download it:

    Start-WindowsCleanup on GitHub
     
  2. abbodi1406

    abbodi1406 MDL KB0000001

    Joined:
    Feb 19, 2011
    Messages:
    17,545
    Likes Received:
    93,116
    Trophy Points:
    340
    The issues actually start with build 18362 (v1903), 18363 is just registry version for 1909 :)
     
  3. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    Thanks for the clarification. I changed it from 18363 to 18362.
     
  4. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    Added optional Microsoft Edge cache directory clean-up to the -Include parameter set.
     
  5. vigipirate

    vigipirate MDL Senior Member

    Joined:
    Feb 24, 2011
    Messages:
    405
    Likes Received:
    101
    Trophy Points:
    10
    please create broser bat or cmd
     
  6. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    What?
     
  7. coleoptere2007

    coleoptere2007 MDL Guru

    Joined:
    Apr 8, 2008
    Messages:
    3,302
    Likes Received:
    1,940
    Trophy Points:
    120
    the guy ask you to create a batch or cmd file :laie:
     
  8. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    That's not going to happen.
     
  9. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    Updated with clean-up improvements.
     
  10. rayleigh_otter

    rayleigh_otter MDL Expert

    Joined:
    Aug 8, 2018
    Messages:
    1,120
    Likes Received:
    933
    Trophy Points:
    60
    I would love to see bat/cmd versions(cos i know my way around them) of powersmell stuff but you have to accept and respect a developers choice. We have 2 choices, try to create our own bats or learn powersmell.
     
  11. MMIKEE

    MMIKEE MDL Senior Member

    Joined:
    Oct 6, 2012
    Messages:
    380
    Likes Received:
    361
    Trophy Points:
    10
    GUI's <and / or> bat/cmd scripts to be able to use @GodHand powershell scripts would be Greatly Appreciated since NOT every MDL member is a powershell expert or even remotely close to being one...
     
  12. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    You do not have to be anywhere close to a "PowerShell expert" to run any of the functions I post - that's why I post them.

    Functions can be copied and then pasted right into an elevated PowerShell console and then executed by typing the function name, or they can be saved saved to a .ps1 file and called directly within a PowerShell console like you'd call one batch script from another.
     
  13. rayleigh_otter

    rayleigh_otter MDL Expert

    Joined:
    Aug 8, 2018
    Messages:
    1,120
    Likes Received:
    933
    Trophy Points:
    60
    Knowing something works isnt enough for me, i like to know how and why, thats how i learnt the basics of bat's and cmd's.
     
  14. rayleigh_otter

    rayleigh_otter MDL Expert

    Joined:
    Aug 8, 2018
    Messages:
    1,120
    Likes Received:
    933
    Trophy Points:
    60
    Just out of interest @GodHand, do you know of a tool that converts bats and cmds to powershell? I prefer to use one type of file only and combine stuff into 1 large script rather than seperate bats/cmds and powershells.
     
  15. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    #15 GodHand, Nov 10, 2019
    Last edited: Nov 10, 2019
    (OP)
    There is no tool that can do that. PowerShell code logic and syntax are completely different and you can't just convert it - you have to rewrite it.

    You can execute bat/cmd scripts within PowerShell and monitor them using many PowerShell's cmdlets, though, which is not hard. Likewise, you can encode PowerShell scripts and execute them within bat/cmd scripts.

    If you are interested in learning and knowing the 'how and why,' why are you asking me to create a version in a depreciated format for you instead of taking the time to look at the code to understand exactly what it's doing?

    It's not a complicated function.
     
  16. rayleigh_otter

    rayleigh_otter MDL Expert

    Joined:
    Aug 8, 2018
    Messages:
    1,120
    Likes Received:
    933
    Trophy Points:
    60
    Im not. You create a PS script in that format only as is your right to do so i want to learn PS so i know what you do and how you do it. Im backing you on your choice of PS scripts only, im on your side. :)
     
  17. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    Updated 11-14-2019: Added additional logging and update clean-up locations. Tweaked the preclean-up and post clean-up results that get returned. Added some code changes and optimizations.
     
  18. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    Updated 01-16-2020:
    - Added additional directories for removal.
    - Added methods to clean-up the component store and/or reset the image base using a background job.
    - Added a timed pop-up verification for the removal of download folders' contents in case it was accidentally entered when calling the function.
    - Assorted other code changes.
     
  19. Windows_Addict

    Windows_Addict MDL Expert

    Joined:
    Jul 19, 2018
    Messages:
    1,393
    Likes Received:
    4,643
    Trophy Points:
    60
    @vigipirate @rayleigh_otter @MMIKEE

    Batch script to call Powershell function,

    Save the Powershell (.ps1) script and batch script (.cmd) in the same directory.

    Code:
    @echo off
    
    reg query HKU\S-1-5-19 1>nul 2>nul || (
    echo ==== Error ====
    echo Right click on this file and select 'Run as administrator'
    echo Press any key to exit...
    pause >nul
    exit /b
    )
    
    REM                                                   File name                 Function name
    powershell -ExecutionPolicy ByPass -command ". "%~dp0Start-WindowsCleanup.ps1"; Start-WindowsCleanup;"
    pause
    It works with GodHand's other script too,
    P.S. To avoid any possible errors, make sure that the pathname does not have spaces or special characters.
     
  20. GodHand

    GodHand MDL Addicted

    Joined:
    Jul 15, 2016
    Messages:
    534
    Likes Received:
    926
    Trophy Points:
    30
    Updated 02-13-2020
    - Multiple code improvements.