Start-WindowsCleanup

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

  1. verndog

    verndog MDL Junior Member

    May 3, 2010
    79
    12
    0
    I ran the above parameters, it ran but at the beinning I got this (the bottom of code tags starts the cleanup):
    Code:
    Transcript started, output file is C:\Windows\Temp\Start-WindowsCleanup_1592955574.log
    The variable '$BuildNumber' cannot be retrieved because it has not been set.
    At C:\Users\user\documents\Start-WindowsCleanup.ps1:310 char:135
    + ... ant to reset the image base for build [{0}]?' -f $BuildNumber, 'Verif ...
    +                                                      ~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (BuildNumber:String) [], RuntimeException
        + FullyQualifiedErrorId : VariableIsUndefined
    
    Get-Content : Cannot find path 'C:\Users\User\AppData\Local\Temp\DismCleanupImage_1592955574.log' because it does not
    exist.
    At C:\Users\user\documents\Start-WindowsCleanup.ps1:335 char:17
    +                 Get-Content -Path $DISMLog -Tail 3 | Select-String -P ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (C:\Users\User\A..._1592955574.log:String) [Get-Content], ItemNotFoundEx
       ception
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
    
    The variable '$DISMJob' cannot be retrieved because it has not been set.
    At C:\Users\user\documents\Start-WindowsCleanup.ps1:337 char:33
    +             While ((Get-Job -Id $DISMJob.Id).State -eq 'Running')
    +                                 ~~~~~~~~
        + CategoryInfo          : InvalidOperation: (DISMJob:String) [], RuntimeException
        + FullyQualifiedErrorId : VariableIsUndefined
    
    The variable '$DISMJob' cannot be retrieved because it has not been set.
    At C:\Users\user\documents\Start-WindowsCleanup.ps1:339 char:30
    +             If ((Get-Job -Id $DISMJob.Id).State -eq 'Completed') { $D ...
    +                              ~~~~~~~~
        + CategoryInfo          : InvalidOperation: (DISMJob:String) [], RuntimeException
        + FullyQualifiedErrorId : VariableIsUndefined
    
    The variable '$DISMJob' cannot be retrieved because it has not been set.
    At C:\Users\user\documents\Start-WindowsCleanup.ps1:341 char:13
    +             $DISMJob | Remove-Job -ErrorAction SilentlyContinue
    +             ~~~~~~~~
        + CategoryInfo          : InvalidOperation: (DISMJob:String) [], RuntimeException
        + FullyQualifiedErrorId : VariableIsUndefined
    
    VERBOSE: Performing the operation "ClearLog" on target "Windows PowerShell"
    VERBOSE: Performing the operation "ClearLog" on target "System"
    VERBOSE: Performing the operation "ClearLog" on target "Security"
    ...then..the cleanup finished...
     
  2. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    11,260
    48,624
    340
    Open Start-WindowsCleanup.ps1 with notepad, search for this line
    Code:
    # Before performing an image base reset on a Windows build greater than 18362, which can cause future updates from failing to install, request verification
    and add this line after it
    Code:
    $BuildNumber = Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber
    or wait for @GodHand to fix it
     
  3. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    529
    834
    30
    This should be corrected now, though there will be a pretty major update to this coming shortly. I just have not had the time to thoroughly test everything.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    529
    834
    30
    Updated 06-30-2020
    - The Start-WindowsCleanup is now a script module. To run it, you must first import the module using Import-Module .\Start-WindowsCleanup.psm1
    - Using the new optional -JSON switch, the VolumeCaches.json file will be used to specify what clean-up components are processed by simply changing the 'Clean' parameter to a True/False value.
    - If the -JSON switch is not passed when calling Start-WindowsCleanup, the same dynamically created list as previous versions will be used instead, so the use of the VolumeCaches.json is entirely optional.
    - Added an 'Analyze' value to the -ComponentStore parameter.
    - The 'Analyze' value can be used with any of the other two -ComponentStore parameters, and will analyze the component store, return whether a clean-up of the component store is recommended or not, and allow you to select whether the clean-up or image base reset is performed.
    - Any open directory paths are now saved prior to processing any clean-up tasks and then restored once all processes have completed.
    - The creation of the .NET process for the Windows Disk Clean-up utility has been added to a Try/Catch block that will instead run the Windows Disk Clean-up utility using the native Start-Process function if any errors occur when creating the .NET process (this is very rare).
    - The clean-up of the icon cache databases will now give the option to restart the system upon completion in order to rebuild the cache databases.
    - All header information and examples have been updated, so browse that data before running the script.

    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
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. zbigniew59

    zbigniew59 MDL Member

    May 14, 2016
    111
    21
    10
    #65 zbigniew59, Jul 15, 2020
    Last edited: Jul 17, 2020
    removed
     
  6. zbigniew59

    zbigniew59 MDL Member

    May 14, 2016
    111
    21
    10
    Sorry, but I don't know too much - can someone write straight how to run this scrypt now?
     
  7. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    529
    834
    30
    #67 GodHand, Aug 3, 2020
    Last edited: Aug 3, 2020
    (OP)
    08/03/2020
    - Added Windows Defender clean-up to the -Include and -GUI parameters.
    - Reverted it back to a regular script (.ps1) file for simplicity for users who may not know how to load/unload function modules.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    529
    834
    30
    Go to the Start-WindowsCleanup Github page and click the 'Download Zip' button. Then unzip the file, open an elevated PowerShell console in the same directory it was saved it and type:

    Code:
    .\Start-WindowsCleanup.ps1
    That will load the Start-WindowsCleanup function into your current session where you can use it.

    Or you can just copy and paste the contents from the Start-WindowsCleanup function on its Github page, paste it into an elevated PowerShell console (or PowerShell ISE) and hit enter.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. zbigniew59

    zbigniew59 MDL Member

    May 14, 2016
    111
    21
    10
  10. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    529
    834
    30
    It worked when you dot-sourced it properly (without adding an additional space) by typing:

    Code:
    .\Start-WindowsCleanup.ps1
    Hence why there's no error (or anything) after you issued that command. When you did that, it loaded the Start-WindowsCleanup function into your current PowerShell session. From there you just need to execute the function with whatever parameters you want, i.e.:

    Code:
    Start-WindowsCleanup -GUI
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. zbigniew59

    zbigniew59 MDL Member

    May 14, 2016
    111
    21
    10
  12. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    529
    834
    30
    Code:
    . .\Start-WindowsCleanup.ps1
    Start-WindowsCleanup -GUI
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    529
    834
    30
    Updated 08/21/2020
    - The clean-up of the icon cache databases no longer requires a system reboot for them to be rebuilt.
    - The Explorer process is now stopped before removals are processed and restarted after removals have completed. This is to ensure items that are in use by the running system can be removed without requiring a system reboot.
    - Updated helper functions.
    - Additional function improvements.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    529
    834
    30
    Updated 08/22/2020
    - The existence of a pending.xml file is now checked before continuing with a clean-up of the Component Store.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. Stafamu

    Stafamu MDL Novice

    Sep 22, 2018
    23
    7
    0
    #76 Stafamu, Aug 22, 2020
    Last edited: Aug 22, 2020
    @zbigniew59 save code below as WindowsCleanup.cmd and put on C drive along with ps1 script
    It works more than fine. Your numerous replies say nothing constructive except it is not working for you. It works more than fine.
    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
    )
    
    set "_work=%~dp0"
    setlocal EnableDelayedExpansion
    pushd "!_work!"
    REM                                   File name                 Function name
    powershell -nop -ep bypass -c "& {. .\Start-WindowsCleanup.ps1; Start-WindowsCleanup -GUI;}"
     
  16. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    529
    834
    30
    #77 GodHand, Aug 22, 2020
    Last edited: Aug 22, 2020
    (OP)
    You are not dot-sourcing it properly...

    Code:
    . .\Start-WindowsCleanup.ps1
    
    There is a space between the first period and the second. The first is the actual dot-source while the second tells the PowerShell session that the script function is in the same root path the session was started in.

    Or, if it's in a different directory, you can enter:

    Code:
    . C:\Path\To\Script.ps1
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    529
    834
    30
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  18. Makinenn

    Makinenn MDL Junior Member

    Dec 7, 2010
    60
    26
    0
    Hi,

    Is this for Windows 10 only or can it be used on Windows 7?
    Sorry if it's an obvious question, but I couldn't find any specific mention to it.
     
  19. TairikuOkami

    TairikuOkami MDL Expert

    Mar 15, 2014
    1,005
    867
    60
    ResetBase still breaks windows updates, I had to reinstall 19041.423.