Clean Windows C Volume

Have you ever ran into a sluggish performance on a Windows machine that is being shared by many [remote] users? Chances are that each user has lots of junk files that would be saved in certain important volumes, such as C:\ drive. Thus, I’ve put together these spaghetti lines to automate maintenance of these machines.

# cleanCVolume.ps1

## Variables Declaration ####
    "$env:SystemDrive\Windows\Prefetch\", # $prefetchData
    "$env:SystemDrive\Windows\Logs\CBS\", # $cbs
    "$env:SystemDrive\swtools\", # $swtools
    "$env:SystemDrive\drivers\", # $drivers
    "$env:SystemDrive\swsetup\", # $swsetup
    "$env:SystemDrive\Windows\SoftwareDistribution\Download\", #$softwareDistribution

function purgeDirectory($path='c:\temp'){     
    function confirmation($content,$testValue="I confirm",$maxAttempts=3){
            write-host "Please review this content for accuracy.`r`n"
            while ($attempts -le $maxAttempts){
                if($attempts++ -ge $maxAttempts){
                    write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
                $userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item.";
                if ($userInput.ToLower() -eq $testValue.ToLower()){
                    write-host "Confirmed!`r`n";
                }elseif($userInput -like 'cancel'){
                    write-host 'Cancel command received.'
                    write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again or Input CANCEL to skip this item`r`n"
            return $confirmed;
    write-host "Granting Administrators full access to $path..."
        # $null=takeown /F $path /A /R /D Y
        $null=icacls $path /Grant Administrators:F /inheritance:e /T
        write-warning $_
    md -Force $emptyDirectory
    Remove-Item "$emptyDirectory`\*" -force -recurse -ErrorAction Continue
    $confirmed=confirmation "This will delete all files and folders from $path"
    if ($confirmed){
        write-host "Now purging ALL files and folders inside $path..." -foregroundcolor yellow
        $null=robocopy $emptyDirectory $path /mir /R:0 /W:0 /NP
        # rmdir -Force $path
        write-host "Purging has been cancelled for $path"
function cleanUserProfiles{
    $profiles=(get-childitem c:\users -Directory -EA Ignore).Name
        Foreach($profile in $profiles){            
            $tempPath = "C:\Users\$profile\AppData\Local\Temp"
            $downloadPath = "C:\Users\$profile\Downloads"
            purgeDirectory $tempPath
            Write-host "$tempPath cleared."
            purgeDirectory $downloadPath
            Write-host "$downloadPath cleared."

function cleanWindows{
    foreach($directoryToPurge in $directoriesToPurge){
        purgeDirectory $directoryToPurge
    write-host "Clearing all superseded versions of every component in the component store...."
    # This also removes any backup components needed for uninstallation of service packs already installed
    Dism.exe /online /Cleanup-Image /startcomponentcleanup /resetbase


Leave a Reply

Your email address will not be published. Required fields are marked *