Files
management-scripts/Cleanup-SystemTempFiles.ps1
T

74 lines
3.0 KiB
PowerShell
Raw Normal View History

2020-09-28 15:14:18 -04:00
## Source the tools script if it isn't already available
If ( !($MSPName) ) { . $([Scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1'))) }
## Set the name of the log file
$LogName = 'Cleanup-SystemTempFiles'
## 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-09-28 15:14:18 -04:00
Log "Running with administrative privileges" -Name $LogName
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-09-28 15:14:18 -04:00
Log "Deleting all event logs from ${SysTemp}" -Name $LogName
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-09-28 15:14:18 -04:00
Log "Deleting misc logs from ${SysTemp}" -Name $LogName
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-09-28 15:14:18 -04:00
Log "Deleting archived CBS logs from ${Env:SystemRoot}\Logs\CBS" -Name $LogName
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-09-28 15:14:18 -04:00
Log "Deleting all temp files not modified in ${OlderThan} day(s) from ${SysTemp}" -Name $LogName
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-09-28 15:14:18 -04:00
Log "Turning off system hibernation" -Name $LogName
2020-09-17 15:57:43 -04:00
Try { Start-Process "powercfg" -ArgumentList "-h off" -Wait }
2020-09-28 15:14:18 -04:00
Catch { $_.Exception.Message | Log -Error -Name $LogName }
2020-09-17 15:57:43 -04: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-09-28 15:14:18 -04:00
Log "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-09-28 15:14:18 -04:00
Log "Deleting all temp files not modified in ${OlderThan} day(s) from ${Env:TEMP}" -Name $LogName
ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile -Force -Recurse -ErrorAction SilentlyContinue }
}
2020-09-07 20:47:09 -04:00
}