Update Clear-TempFiles and Clear-RecycleBinFiles functions
- Temp files: Only delete items older than 3 days (safer) - Recycle bin: Purge all user trash cans via BitBucket registry keys - Updated menu text to reflect 3-day age filter
This commit is contained in:
1 parent
d276b8fe95
commit
990b147c47
1 file changed
+66
-12
@@ -208,7 +208,7 @@ function Show-Menu {
|
|||||||
if (-not $Silent) {
|
if (-not $Silent) {
|
||||||
Write-Host "`nSelect Remediation Action:" -ForegroundColor Yellow
|
Write-Host "`nSelect Remediation Action:" -ForegroundColor Yellow
|
||||||
Write-Host "1. Clear Windows Update Download Cache" -ForegroundColor White
|
Write-Host "1. Clear Windows Update Download Cache" -ForegroundColor White
|
||||||
Write-Host "2. Purge System & User Temp Files" -ForegroundColor White
|
Write-Host "2. Purge System & User Temp Files (3+ days old)" -ForegroundColor White
|
||||||
Write-Host "3. Clear Windows Error Reporting (WER) Dumps" -ForegroundColor White
|
Write-Host "3. Clear Windows Error Reporting (WER) Dumps" -ForegroundColor White
|
||||||
Write-Host "4. Flush DNS Cache & Reset Network Interfaces" -ForegroundColor White
|
Write-Host "4. Flush DNS Cache & Reset Network Interfaces" -ForegroundColor White
|
||||||
Write-Host "5. Restart Automatic Services That Are Stopped" -ForegroundColor White
|
Write-Host "5. Restart Automatic Services That Are Stopped" -ForegroundColor White
|
||||||
@@ -241,22 +241,55 @@ function Clear-WindowsUpdateCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Clear-TempFiles {
|
function Clear-TempFiles {
|
||||||
param ([string]$TargetComputer = $env:COMPUTERNAME, [Microsoft.Management.Infrastructure.CimSession]$CimSession = $null)
|
param ([string]$TargetComputer = $env:COMPUTERNAME, [Microsoft.Management.Infrastructure.CimSession]$CimSession = $null, [int]$MaxAgeDays = 3)
|
||||||
Write-ColorOutput "`nPurging Temporary Files..." "Yellow" "Info" $TargetComputer
|
Write-ColorOutput "`nPurging Temporary Files (older than $MaxAgeDays days)..." "Yellow" "Info" $TargetComputer
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (-not $CimSession -and $TargetComputer -eq $env:COMPUTERNAME) {
|
if (-not $CimSession -and $TargetComputer -eq $env:COMPUTERNAME) {
|
||||||
$tempPaths = @(
|
$tempPaths = @(
|
||||||
"$env:SystemRoot\Temp\*",
|
"$env:SystemRoot\Temp",
|
||||||
"$env:LOCALAPPDATA\Temp\*",
|
"$env:LOCALAPPDATA\Temp"
|
||||||
"C:\Users\*\AppData\Local\Temp\*"
|
|
||||||
)
|
)
|
||||||
foreach ($path in $tempPaths) {
|
foreach ($path in $tempPaths) {
|
||||||
Remove-Item -Path $path -Recurse -Force -ErrorAction Stop
|
if (Test-Path $path) {
|
||||||
|
Get-ChildItem -Path $path -Recurse -Force -ErrorAction SilentlyContinue |
|
||||||
|
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$MaxAgeDays) } |
|
||||||
|
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$userTempPaths = Get-ChildItem -Path "C:\Users" -Directory -ErrorAction SilentlyContinue |
|
||||||
|
ForEach-Object { "$($_.FullName)\AppData\Local\Temp" }
|
||||||
|
foreach ($path in $userTempPaths) {
|
||||||
|
if (Test-Path $path) {
|
||||||
|
Get-ChildItem -Path $path -Recurse -Force -ErrorAction SilentlyContinue |
|
||||||
|
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$MaxAgeDays) } |
|
||||||
|
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
$scriptBlock = @"
|
||||||
|
`$tempPaths = @("$env:SystemRoot\Temp", "$env:LOCALAPPDATA\Temp")
|
||||||
|
`$maxAge = (Get-Date).AddDays(-$MaxAgeDays)
|
||||||
|
foreach (`$path in `$tempPaths) {
|
||||||
|
if (Test-Path `$path) {
|
||||||
|
Get-ChildItem -Path `$path -Recurse -Force -ErrorAction SilentlyContinue |
|
||||||
|
Where-Object { `$_.LastWriteTime -lt `$maxAge } |
|
||||||
|
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`$userTempPaths = Get-ChildItem -Path "C:\Users" -Directory -ErrorAction SilentlyContinue |
|
||||||
|
ForEach-Object { "`$($_.FullName)\AppData\Local\Temp" }
|
||||||
|
foreach (`$path in `$userTempPaths) {
|
||||||
|
if (Test-Path `$path) {
|
||||||
|
Get-ChildItem -Path `$path -Recurse -Force -ErrorAction SilentlyContinue |
|
||||||
|
Where-Object { `$_.LastWriteTime -lt `$maxAge } |
|
||||||
|
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"@
|
||||||
Invoke-CimMethod -CimSession $CimSession -ClassName Win32_Process -MethodName Create -Arguments @{
|
Invoke-CimMethod -CimSession $CimSession -ClassName Win32_Process -MethodName Create -Arguments @{
|
||||||
CommandLine = 'cmd.exe /c del /f /s /q %systemroot%\Temp\* & del /f /s /q %TEMP%\*'
|
CommandLine = "powershell.exe -Command `$scriptBlock"
|
||||||
} | Out-Null
|
} | Out-Null
|
||||||
}
|
}
|
||||||
Write-ColorOutput "System and user temporary files purged." "Green" "Good" $TargetComputer
|
Write-ColorOutput "System and user temporary files purged." "Green" "Good" $TargetComputer
|
||||||
@@ -335,17 +368,38 @@ function Repair-StoppedAutomaticServices {
|
|||||||
|
|
||||||
function Clear-RecycleBinFiles {
|
function Clear-RecycleBinFiles {
|
||||||
param ([string]$TargetComputer = $env:COMPUTERNAME, [Microsoft.Management.Infrastructure.CimSession]$CimSession = $null)
|
param ([string]$TargetComputer = $env:COMPUTERNAME, [Microsoft.Management.Infrastructure.CimSession]$CimSession = $null)
|
||||||
Write-ColorOutput "`nEmptying Recycle Bin..." "Yellow" "Info" $TargetComputer
|
Write-ColorOutput "`nEmptying Recycle Bin (All Users)..." "Yellow" "Info" $TargetComputer
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (-not $CimSession -and $TargetComputer -eq $env:COMPUTERNAME) {
|
if (-not $CimSession -and $TargetComputer -eq $env:COMPUTERNAME) {
|
||||||
|
$sids = Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume" -ErrorAction SilentlyContinue |
|
||||||
|
ForEach-Object { $_.PSChildName }
|
||||||
|
foreach ($sid in $sids) {
|
||||||
|
$path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume\$sid"
|
||||||
|
if (Test-Path $path) {
|
||||||
|
Get-ChildItem -Path $path -Recurse -ErrorAction SilentlyContinue |
|
||||||
|
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
Clear-RecycleBin -Force -ErrorAction SilentlyContinue
|
Clear-RecycleBin -Force -ErrorAction SilentlyContinue
|
||||||
} else {
|
} else {
|
||||||
|
$scriptBlock = @"
|
||||||
|
`$sids = Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume" -ErrorAction SilentlyContinue |
|
||||||
|
ForEach-Object { `$_.PSChildName }
|
||||||
|
foreach (`$sid in `$sids) {
|
||||||
|
`$path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume\$sid"
|
||||||
|
if (Test-Path `$path) {
|
||||||
|
Get-ChildItem -Path `$path -Recurse -ErrorAction SilentlyContinue |
|
||||||
|
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Clear-RecycleBin -Force -ErrorAction SilentlyContinue
|
||||||
|
"@
|
||||||
Invoke-CimMethod -CimSession $CimSession -ClassName Win32_Process -MethodName Create -Arguments @{
|
Invoke-CimMethod -CimSession $CimSession -ClassName Win32_Process -MethodName Create -Arguments @{
|
||||||
CommandLine = 'powershell.exe -Command "Clear-RecycleBin -Force -ErrorAction SilentlyContinue"'
|
CommandLine = "powershell.exe -Command `$scriptBlock"
|
||||||
} | Out-Null
|
} | Out-Null
|
||||||
}
|
}
|
||||||
Write-ColorOutput "Recycle Bin emptied." "Green" "Good" $TargetComputer
|
Write-ColorOutput "Recycle Bin emptied for all users." "Green" "Good" $TargetComputer
|
||||||
} catch {
|
} catch {
|
||||||
Write-ColorOutput "Failed to empty Recycle Bin: $_" "Red" "Critical" $TargetComputer
|
Write-ColorOutput "Failed to empty Recycle Bin: $_" "Red" "Critical" $TargetComputer
|
||||||
}
|
}
|
||||||
@@ -381,7 +435,7 @@ function Run-AllRemediations {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Clear-WindowsUpdateCache -TargetComputer $TargetComputer -CimSession $cimSession
|
Clear-WindowsUpdateCache -TargetComputer $TargetComputer -CimSession $cimSession
|
||||||
Clear-TempFiles -TargetComputer $TargetComputer -CimSession $cimSession
|
Clear-TempFiles -TargetComputer $TargetComputer -CimSession $cimSession -MaxAgeDays 3
|
||||||
Clear-WerDumps -TargetComputer $TargetComputer -CimSession $cimSession
|
Clear-WerDumps -TargetComputer $TargetComputer -CimSession $cimSession
|
||||||
Reset-NetworkStack -TargetComputer $TargetComputer -CimSession $cimSession
|
Reset-NetworkStack -TargetComputer $TargetComputer -CimSession $cimSession
|
||||||
Repair-StoppedAutomaticServices -TargetComputer $TargetComputer -CimSession $cimSession
|
Repair-StoppedAutomaticServices -TargetComputer $TargetComputer -CimSession $cimSession
|
||||||
|
|||||||
Reference in new issue
Block a user