Files
management-scripts/Cleanup-SystemTempFiles.ps1
T
2020-09-07 20:52:45 -04:00

30 lines
1.3 KiB
PowerShell

$SysTemp = "${Env:SystemRoot}\TEMP"
## Remove runaway AppX logs;n
If ( (IsWindowsVersion -ge "6.2") )
{
Write-Output "Deleting old AppX log files from ${SysTemp}"
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "AppXDeploymentServer_*.evtx" } | Remove-Item -Force
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "AppxErrorReport_*.txt" } | Remove-Item -Force
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "AppXPackaging_*.evtx" } | Remove-Item -Force
}
## Remove runaway CBS logs
$CBSLogDir = "${Env:SystemRoot}\Logs\CBS"
If ( Test-Path $CBSLogDir )
{
Write-Output "Deleting old CBS log files from ${CBSLogDir}"
Get-ChildItem -Path $CBSLogDir -File | Where { ( $_.Name -like "CbsPersist_*.log" ) -or ( $_.Name -like "CbsPersist_*.cab" ) } | Remove-Item -Force
}
## Remove all system temp files older than the specified number of days
$OlderThan=7
$timedelta = New-TimeSpan -Days $OlderThan
Write-Output "Deleting all files older than ${OlderThan} day(s) from ${SysTemp}"
Foreach ( $item in (Get-ChildItem -Path "${SysTemp}") )
{
## If it's older than the number of days specified, recursively delete the directory
If ( $item.LastWriteTime -lt ((Get-Date) - $timedelta) ) { Remove-Item $item.FullName -Recurse -ErrorAction SilentlyContinue -Force }
}