Files
management-scripts/Cleanup-SystemTempFiles.ps1
T

37 lines
2.1 KiB
PowerShell
Raw Normal View History

2020-09-07 20:52:45 -04:00
$SysTemp = "${Env:SystemRoot}\TEMP"
## Remove runaway AppX logs;n
2020-09-07 20:47:09 -04:00
If ( (IsWindowsVersion -ge "6.2") )
{
2020-09-07 20:52:45 -04:00
Write-Output "Deleting old AppX log files from ${SysTemp}"
2020-09-08 22:23:52 -04:00
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "AppXDeploymentServer_*.evtx" } | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "AppxErrorReport_*.txt" } | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "AppXPackaging_*.evtx" } | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "Application_*.evtx" } | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "System_*.evtx" } | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "Microsoft-Windows-WindowsUpdateClient_*.evtx" } | Remove-Item -Force -ErrorAction SilentlyContinue
2020-09-07 20:47:09 -04:00
}
2020-09-08 22:23:52 -04:00
## Remove computer log files
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "Microsoft-Windows-AppReadiness_Admin_*.evtx" } | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path $SysTemp | Where { $_.Name -like "${Env:COMPUTERNAME}-*.log" } | Remove-Item -Force -ErrorAction SilentlyContinue
2020-09-07 20:47:09 -04:00
## Remove runaway CBS logs
2020-09-07 20:52:45 -04:00
$CBSLogDir = "${Env:SystemRoot}\Logs\CBS"
If ( Test-Path $CBSLogDir )
{
Write-Output "Deleting old CBS log files from ${CBSLogDir}"
2020-09-08 22:23:52 -04:00
Get-ChildItem -Path $CBSLogDir -File | Where { ( $_.Name -like "CbsPersist_*.log" ) -or ( $_.Name -like "CbsPersist_*.cab" ) } | Remove-Item -Force -ErrorAction SilentlyContinue
2020-09-07 20:52:45 -04:00
}
2020-09-07 20:47:09 -04:00
## Remove all system temp files older than the specified number of days
$OlderThan=7
$timedelta = New-TimeSpan -Days $OlderThan
2020-09-08 22:17:51 -04:00
Write-Output "Deleting all files more than ${OlderThan} day(s) old ${SysTemp}"
2020-09-07 20:52:45 -04:00
Foreach ( $item in (Get-ChildItem -Path "${SysTemp}") )
2020-09-07 20:47:09 -04:00
{
## If it's older than the number of days specified, recursively delete the directory
2020-09-08 22:23:52 -04:00
If ( $item.LastWriteTime -lt ((Get-Date) - $timedelta) ) { Remove-Item $item.FullName -Recurse -ErrorAction SilentlyContinue -Force -ErrorAction SilentlyContinue }
2020-09-07 20:47:09 -04:00
}