# When deleting temp files en masse, only delete files/folders older than the number of days specified here $OlderThan = 7 $TimeDelta = New-TimeSpan -Days $OlderThan # Only run this section if we have administrative privileges If ( IsAdmin ) { LogMsg "Running with administrative privileges" # Remove all *.evtx files from $SysTemp $EventLogs = Get-ChildItem -Path "${Env:SystemRoot}\TEMP\*" -File -Filter *.evtx If ( $EventLogs ) { LogMsg "Deleting all event logs from ${Env:SystemRoot}\TEMP" ForEach ( $EventLog in $EventLogs ) { Remove-Item $EventLog -Force -ErrorAction SilentlyContinue } } # Remove misc logs and error reports $MiscLogs = Get-ChildItem -Path "${Env:SystemRoot}\TEMP\*" -File -Include "${Env:COMPUTERNAME}-*.log", "AppxErrorReport_*.txt" If ( $MiscLogs ) { LogMsg "Deleting misc logs from ${Env:SystemRoot}\TEMP" ForEach ( $MiscLog in $MiscLogs ) { Remove-Item $MiscLog -Force -ErrorAction SilentlyContinue } } # Remove archived CBS logs $CBSLogs = Get-ChildItem -Path "${Env:SystemRoot}\Logs\CBS\*" -File -Include "CbsPersist_*.log", "CbsPersist_*.cab" If ( $CBSLogs ) { $TotalBytes = 0 ForEach ( $CBSLog in $CBSLogs) { $TotalBytes += $CBSLog.Length } $TotalSize = $TotalBytes / 1MB If ( $TotalSize -ge 200 ) { LogMsg "Total CBS log archive size: ${TotalSize}MB" Control-Service -Stop 'TrustedInstaller' LogMsg "Deleting primary CBS.log file" Remove-Item "${Env:SystemRoot}\Logs\CBS\CBS.log" -Force -ErrorAction SilentlyContinue } Control-Service -Start 'TrustedInstaller' } LogMsg "Deleting archived CBS logs from ${Env:SystemRoot}\Logs\CBS" ForEach ( $CBSLog in $CBSLogs ) { Remove-Item $CBSLog -Force -ErrorAction SilentlyContinue } } # Remove all system temp files older than 7 days $OldTempFiles = Get-ChildItem -Path "${Env:SystemRoot}\TEMP" | Where { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) } If ( $OldTempFiles ) { LogMsg "Deleting all temp files not modified in ${OlderThan} day(s) from ${Env:SystemRoot}\TEMP" ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile -Force -Recurse -ErrorAction SilentlyContinue } } # Turn off system hibernation if it's in use If ( Test-Path "${Env:SystemDrive}\hiberfil.sys" ) { LogMsg "Turning off system hibernation" Try { Start-Process "powercfg" -ArgumentList "-h off" -Wait } Catch { LogErr $_.Exception.Message } } # Remove PDCRevocation dump files $PDCRevocationDumpFiles = Get-ChildItem -Path "${Env:SystemRoot}\LiveKernelReports\*" -File -Include "PDCRevocation-*.dmp" If ( $PDCRevocationDumpFiles ) { LogMsg "Deleting PDCRevocation dump files from ${Env:SystemRoot}\LiveKernelReports" ForEach ( $PDCRevocationDump in $PDCRevocationDumpFiles ) { Remove-Item $PDCRevocationDump -Force -ErrorAction SilentlyContinue } } # Remove Genetec application crash dumps If ( Test-Path "${Env:ProgramData}\Genetec\Dumps" ) { $GenetecCrashDumps = Get-ChildItem -Path "${Env:ProgramData}\Genetec\Dumps" If ( $GenetecCrashDumps ) { LogMsg "Deleting Genetec application crash dumps" Try { ForEach ( $dump in $GenetecCrashDumps ) { Remove-Item $dump.FullName -Force -Recurse -ErrorAction SilentlyContinue } } Catch { LogErr $_.Exception.Message } } } # Remove AutoCAD temp files If (-not (IsRunning -Name "acad") ) { $AutoCADTempFiles = Get-ChildItem -Path "${Env:SystemDrive}\Users" | Where-Object { $_.PSIsContainer -eq $true } | ForEach-Object { Get-ChildItem -Path $($_.FullName + "\AppData\Local\Temp\*") -File -Include '*.ac$' -ErrorAction SilentlyContinue } If ( $AutoCADTempFiles ) { LogMsg "Deleting AutoCAD temp files for all users" ForEach ( $tempfile in $AutoCADTempFiles ) { Remove-Item $tempfile -Force -ErrorAction SilentlyContinue } } } # Silently run cleanmgr.exe with default settings LogMsg "Running cleanmgr.exe with default settings" Try { Start-Process "cleanmgr.exe" -ArgumentList "/VERYLOWDISK" } Catch { LogErr $_.Exception.Message } } # Only run this section if we don't have administrative privileges Else { LogMsg "Running without administrative privileges" # Remove all system temp files older than 7 days $OldTempFiles = Get-ChildItem -Path $Env:TEMP | Where { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) } If ( $OldTempFiles ) { LogMsg "Deleting all temp files not modified in ${OlderThan} day(s) from ${Env:TEMP}" ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile -Force -Recurse -ErrorAction SilentlyContinue } } # Remove AutoCAD temp files If (-not (IsRunning -Name "acad") ) { $AutoCADTempFiles = Get-ChildItem -Path "${Env:TEMP}\*" -File -Include '*.ac$' -ErrorAction SilentlyContinue If ( $AutoCADTempFiles ) { LogMsg "Deleting AutoCAD temp files" ForEach ( $tempfile in $AutoCADTempFiles ) { Remove-Item $tempfile -Force -ErrorAction SilentlyContinue } } } # Silently run cleanmgr.exe with default settings LogMsg "Running cleanmgr.exe with default settings" Try { Start-Process "cleanmgr.exe" -ArgumentList "/VERYLOWDISK" } Catch { LogErr $_.Exception.Message } }