avatar
Untitled

Guest 238 21st Nov, 2022

MARKUP 9.91 KB
                                           
                         Function TempsRemove {

    [CmdletBinding()]
        param (
            [Parameter()]

            [switch]$CleanMgr,

            [Parameter()]
            [switch]$CleanMgr_Deep,

            [Parameter()]
            [switch]$IncludeSystemFiles
        )

    [System.Console]::SetWindowPosition(0,[System.Console]::CursorTop)
    Write-Warning "Temps removing..."

    $Script:ErrorActionPreference = 'SilentlyContinue'

    $Paths = @(
        "C:\Windows\Temp\",
        "C:\Windows\ccmcache",
        "C:\Windows\prefetch",
        "C:\Windows\SoftwareDistribution\"
        "C:\Windows\Downloaded Program Files\",
        "C:\Users\*\AppData\Local\Microsoft\Windows\INetCache\",
        "C:\Users\*\AppData\Local\Microsoft\Windows\INetCookies\",
        "C:\Users\*\AppData\Local\Temp\",
        "C:\Users\*\AppData\Local\Microsoft\Windows\Explorer\",
        "C:\Users\*\AppData\Roaming\Microsoft\Teams\Cache\",
        "C:\Users\*\Appdata\local\Google\Chrome\User Data\Default\Service Worker\CacheStorage\",
        "C:\Users\*\Appdata\local\Microsoft\Edge\User Data\Default\Service Worker\CacheStorage\",
       
       #extras
        
        "C:\windows\logs\software",
        'C:\windows\Installer\$PatchCache$'


    ) | Get-Item -Force | Get-ChildItem -Force | Remove-Item -Force -Recurse

    $REGPath = @(
        "registry::HKEY_USERS\*_Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags\",
        "registry::HKEY_USERS\*_Classes\Local Settings\Software\Microsoft\Windows\Shell\BagMRU\"
    ) |  Get-Item -Force | Remove-Item -Force -Recurse

    #Get-ChildItem $Paths -Force | Remove-Item -Force -Recurse
    #$REGPath | Remove-Item -Force -Recurse

    If($CleanMgr_Deep.IsPresent){

    #################################### CLEAN MGR - CUSTOM CLEAN SETTINGS ##############################################

        #DEFAULT MICROSOFT RECOMANDED MODE
        #Cleanmgr.exe /sagerun:65535 
        #Start-Process "$env:SystemRoot\System32\cleanmgr.exe" -ArgumentList "/sagerun:65535" -WindowStyle Normal #-Wait
    
        $VolumeCachesREG = "registry::\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\"
        $GroupNo = "StateFlags0064"
        $Order = @(
            "Active Setup Temp Folders",                # Temporary Setup Files
            "BranchCache",                              # Branch Cache (WAN bandwidth optimization)  
            "Content Indexer Cleaner",                  # Catalog Files for the Content Indexer (deletes all files in the folder c:\catalog.wci)
            "Delivery Optimization Files",              # Delivery Optimization Files (service to share bandwidth for uploading Windows updates) 
            "Device Driver Packages",                   # Optional Drivers files
            "Diagnostic Data Viewer database Files",    # Diagnostic data viewer database files (Windows app that sends data to Microsoft)
            "D3D Shader Cache",                         # Direct X Shader cache (graphics cache, clearing this can can speed up application load time.)
            "Downloaded Program Files",                 # Downloaded Program Files (ActiveX controls and Java applets downloaded from the Internet)
            #"Downloads Folder",                        # Downloads Folder (Automatically emptying this is probably a bad idea.)
            "Internet Cache Files",                     # Temporary Internet Files 
            "Language Pack",                            # Language resources Files (unused languages and keyboard layouts)
            "Offline Pages Files",                      # Offline Files (Web pages)
            "Old ChkDsk Files",                         # Old ChkDsk Files
            #"Previous Installations"                   # Windows.OLD file ( available after update )
            #"Recycle Bin",                             # Recycle Bin
            "RetailDemo Offline Content",               # Retail Demo
            #"ServicePack Cleanup",                     # Update package Backup Files (old versions)
            "Setup Log Files",                          # Setup Log files (software install logs)
            "System error memory dump files",           # System Error memory dump files (These can be very large if the system has crashed)
            "System error minidump files",              # System Error minidump files (smaller memory crash dumps)
            "Temporary Files",                          # Temporary Files (%Windir%\Temp and %Windir%\Logs)
            "Temporary Setup Files",
            "Temporary Sync Files",
            #"Update Cleanup",                          # Windows Update Cleanup (old system files not migrated during a Windows Upgrade)
            #"User file versions",                      # User file history (Settings > Update & Security > Backup.)
            "Update Cleanup",
            "Upgrade Discarded Files",
            "Thumbnail Cache",                          # Thumbnails (Explorer will recreate thumbnails as each folder is viewed.)
            "Windows Defender",                         # Windows Defender Antivirus
            "Windows Error Reporting Files",            # Windows error reports and feedback diagnostics
            #"Windows ESD installation files",          # Electronic Software Delivery - used to "Reset your PC" to its factory default settings. Don’t clean up this unless you are prepared to Download & re-activate Windows if it crashes.
            "Windows Upgrade Log Files"                 # Windows Upgrade log files

        ) | ForEach-Object { 
                $CombinePath = [IO.Path]::Combine($VolumeCachesREG, $_)
                Set-ItemProperty -Path $CombinePath -Name $GroupNo -Value 2 -Type DWORD -Force #-Confirm  
                #Remove-ItemProperty -Path $CombinePath -Name $GroupNo -Force #-Confirm  
        }

        # $Order | Foreach-object { Set-ItemProperty -Path $_ -Name $GroupNo -Value 2 -Type DWORD -Confirm }
        $args = "/sagerun:64"
        Start-Process -FilePath "$env:SystemRoot\System32\cleanmgr.exe" -ArgumentList $args -WindowStyle Normal #-Wait
        ######################################################### END #######################################################
    }

    If($CleanMgr.IsPresent){

    #DEFAULT MICROSOFT RECOMANDED MODE
        #Cleanmgr.exe /sagerun:65535 
        Start-Process "$env:SystemRoot\System32\cleanmgr.exe" -ArgumentList "/sagerun:65535" -WindowStyle Normal #-Wait
    }

    If($IncludeSystemFiles.IsPresent){

        $LCU = "C:\Windows\servicing\LCU"
        Function RemoveSystemFilesFodlers{

            [CmdletBinding()]
                param (
                    [Parameter(Mandatory)]
                        [String]$Path
                    )
        
            If(Test-Path -Path $Path -PathType Any){
        
                $fileSystemRight = [System.Security.AccessControl.FileSystemRights]::FullControl
                $accessControlType =[System.Security.AccessControl.AccessControlType]::Allow
                $ntAccount = New-Object System.Security.Principal.NTAccount('BUILTIN\Administrators')
                
                if(Test-Path $path -PathType Container){
                    $inheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.InheritanceFlags]::ObjectInherit
                    $propagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly
                }
                else{
                    $inheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None
                    $propagationFlag = [System.Security.AccessControl.PropagationFlags]::None
                }
                
                $newRule = New-Object System.Security.AccessControl.FileSystemAccessRule($ntAccount,$fileSystemRight,$accessControlType)
                $objACL = Get-ACL $path
                
                $objACL.SetAccessRuleProtection($False, $True)
                $objACL.SetOwner($ntAccount)
                $objACL.AddAccessRule($newRule)
                
                $Item = Get-item $path -Force
                $Item  | Set-Acl -AclObject $objACL
                if($Item.PSISContainer){ $Item | Get-childitem -Force -Recurse | Set-Acl -AclObject $objACL}
        
                $Item | Remove-Item -Force -Recurse
        
            }
        }RemoveSystemFilesFodlers -Path $LCU

        $Services = @("wuauserv","bits","cryptSvc","msiserver") | Get-Service
        $Services.Stop()

        Rename-Item "C:\Windows\SoftwareDistribution" -NewName "C:\Windows\SoftwareDistribution.OLD" -Force

        $Paths = @(
            "C:\Windows\SoftwareDistribution",
            "C:\Windows\SoftwareDistribution.OLD",
            "C:\Windows\SoftwareDistribution.bak"
        ) | Get-Item  -EA SilentlyContinue | Remove-Item -Recurse -Force

        $Services.Start()
    }
    
    Write-Host "DONE..." -ForegroundColor Green; Start-Sleep -Milliseconds 1000

}TempsRemove
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes
Ta strona używa plików cookie w celu usprawnienia i ułatwienia dostępu do serwisu oraz prowadzenia danych statystycznych. Dalsze korzystanie z tej witryny oznacza akceptację tego stanu rzeczy.
Wykorzystywanie plików Cookie
Jak wyłączyć cookies?
ROZUMIEM