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 ####
$directoriesToPurge=@(
"$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
'C:\windows\Temp',
'C:\Temp',
'C:\ProgramData\Microsoft\Windows\WER\ReportArchive',
'C:\ProgramData\Microsoft\Windows\WER\ReportQueue',
'C:\ServiceProfiles\LocalService\AppData\Local\Temp'
)
function purgeDirectory($path='c:\temp'){
function confirmation($content,$testValue="I confirm",$maxAttempts=3){
$confirmed=$false;
$attempts=0;
$content|write-host
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"
break;
}
$userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item.";
if ($userInput.ToLower() -eq $testValue.ToLower()){
$confirmed=$true;
write-host "Confirmed!`r`n";
break;
}elseif($userInput -like 'cancel'){
write-host 'Cancel command received.'
$confirmed=$false
break
}else{
cls;
$content|write-host
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..."
try{
# $null=takeown /F $path /A /R /D Y
$null=icacls $path /Grant Administrators:F /inheritance:e /T
}catch{
write-warning $_
continue
}
$emptyDirectory="C:\emptyDirectory"
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
}else{
write-host "Purging has been cancelled for $path"
}
}
function cleanUserProfiles{
$profiles=(get-childitem c:\users -Directory -EA Ignore).Name
if($profiles){
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
}
cleanUserProfiles
cleanWindows