Files
management-scripts/Cleanup-SystemTempFiles.ps1
T

79 lines
3.1 KiB
PowerShell
Raw Normal View History

2020-11-18 21:34:17 -05:00
## Path to the system temp folder
$SysTemp = "${Env:SystemRoot}\TEMP"
2020-09-07 20:52:45 -04:00
## 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 )
2020-09-07 20:47:09 -04:00
{
2020-10-22 14:01:35 -04:00
LogMsg "Running with administrative privileges"
2020-09-07 20:47:09 -04:00
## Remove all *.evtx files from $SysTemp
$EventLogs = Get-ChildItem -Path "${SysTemp}\*" -File -Filter *.evtx
If ( $EventLogs )
{
2020-10-22 14:01:35 -04:00
LogMsg "Deleting all event logs from ${SysTemp}"
ForEach ( $EventLog in $EventLogs ) { Remove-Item $EventLog -Force -ErrorAction SilentlyContinue }
}
2020-09-08 22:23:52 -04:00
## Remove misc logs and error reports
$MiscLogs = Get-ChildItem -Path "${SysTemp}\*" -File -Include "${Env:COMPUTERNAME}-*.log", "AppxErrorReport_*.txt"
If ( $MiscLogs )
{
2020-10-22 14:01:35 -04:00
LogMsg "Deleting misc logs from ${SysTemp}"
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 )
{
2020-10-22 14:01:35 -04:00
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 $SysTemp | Where { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
If ( $OldTempFiles )
{
2020-10-22 14:01:35 -04:00
LogMsg "Deleting all temp files not modified in ${OlderThan} day(s) from ${SysTemp}"
ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile -Force -Recurse -ErrorAction SilentlyContinue }
}
2020-09-17 15:57:43 -04:00
## Turn off system hibernation if it's in use
If ( Test-Path "${Env:SystemDrive}\hiberfil.sys" )
{
2020-10-22 14:01:35 -04:00
LogMsg "Turning off system hibernation"
2020-09-17 15:57:43 -04:00
Try { Start-Process "powercfg" -ArgumentList "-h off" -Wait }
2020-10-22 14:01:35 -04:00
Catch { LogErr $_.Exception.Message }
2020-09-17 15:57:43 -04:00
}
2020-11-18 16:42:34 -05:00
## Remove Genetec application crash dumps
If ( Test-Path "${Env:ProgramData}\Genetec\Dumps" ) {
$GenetecCrashDumps = Get-ChildItem -Path "${Env:ProgramData}\Genetec\Dumps" | Where { $_.Name -match "^.*_Crash(\.zip)?$" }
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 }
}
2020-11-18 16:42:34 -05:00
}
2020-09-07 20:52:45 -04:00
}
2020-09-07 20:47:09 -04:00
## Only run this section if we don't have administrative privileges
Else
2020-09-07 20:47:09 -04:00
{
2020-10-22 14:01:35 -04:00
LogMsg "Running without administrative privileges"
2020-09-07 20:47:09 -04:00
## Remove all system temp files older than 7 days
$OldTempFiles = Get-ChildItem -Path $Env:TEMP | Where { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
If ( $OldTempFiles )
{
2020-10-22 14:01:35 -04:00
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 }
}
2020-09-07 20:47:09 -04:00
}