Functions/Public/Invoke-Cleanmgr.ps1

function Invoke-CleanMgr {
    <#
    .SYNOPSIS
        Start "Disk Cleanup" (alos known as cleanmgr) in auto-mode with all options enabled.
    .DESCRIPTION
        Clean Manager allows to clean up your computer. This function starts the application "Disk Cleanup" (cleanmgr.exe) in auto-mode.
        It automatically tick all options in the application and start a cleaning.
        Options ticked: Microsoft Defender Antivirus, Windows upgrade log files, Downloaded Program Files, Temporary Internet Files, DirectX Shader Cache, Delivery Optimization Files, Device driver packages, Language Resource Files, Recyle Bin, Temporary files, Thumbnails, User file history.
    .EXAMPLE
        Invoke-CleanMgr
    .NOTES
        The application "Disk Cleanup" opens 2 Windows during the cleaning. It has no possibility to hide the 2 Windows during the process.
    #>


    [CmdletBinding()]
    Param()

    # Run as admin
    Test-IsAdmin | Out-Null
        
    $FreespaceBefore = (Get-CimInstance -ClassName Win32_Logicaldisk -Filter "DeviceID='C:'" | 
        Select-Object @{Name = "FreeSpace (GB)"; Expression = { [math]::Round($_.Freespace / 1GB, 2) } })

    function Set-SageRun {
        param (
            [int]$StateFlags
        )

        $Number = $StateFlags
        try {
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\D3D Shader Cache' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Delivery Optimization Files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Diagnostic Data Viewer database files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Language Pack' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\RetailDemo Offline Content' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Service Pack Cleanup' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Sync Files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\User file versions' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Defender' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations' -PropertyType 'DWORD' -Force -Name "StateFlags$Number" -Value 0x2
        }
        catch {
            "An error occured"
        }
    }

    $flag = 1234
    Set-SageRun -StateFlags $flag
    Start-Process -FilePath CleanMgr.exe -ArgumentList "/sagerun:$flag" -NoNewWindow -PassThru

    do {
        Clear-Host
        "Waiting for cleanmgr to complete."
        Start-Sleep -Seconds 1
        Clear-Host
        "Waiting for cleanmgr to complete.."
        Start-Sleep -Seconds 1
        Clear-Host
        "Waiting for cleanmgr to complete..."
        Start-Sleep -Seconds 1
    } while ((Get-Process | Where-Object { $_.Name -eq 'cleanmgr' }).Count -gt 0)

    $FreespaceAfter = (Get-CimInstance -ClassName Win32_Logicaldisk -Filter "DeviceID='C:'" | 
        Select-Object @{Name = "FreeSpace (GB)"; Expression = { [math]::Round($_.Freespace / 1GB, 2) } })

    "Free Space Before: {0}" -f $FreespaceBefore
    "Free Space After: {0}" -f $FreespaceAfter

    "The cleaning is complete!"
}