diff --git a/Cleanup-SystemTempFiles.ps1 b/Cleanup-SystemTempFiles.ps1 index ea9050c..ab19054 100644 --- a/Cleanup-SystemTempFiles.ps1 +++ b/Cleanup-SystemTempFiles.ps1 @@ -1,21 +1,27 @@ -## Remove runaway AppX logs;n +$SysTemp = "${Env:SystemRoot}\TEMP" + +## Remove runaway AppX logs;n If ( (IsWindowsVersion -ge "6.2") ) { - Write-Output "Deleting old AppX log files from ${Env:SystemRoot}\TEMP" - Get-ChildItem -Path "${Env:SystemRoot}\TEMP" | Where { $_.Name -like "AppXDeploymentServer_*.evtx" } | Remove-Item -Force - Get-ChildItem -Path "${Env:SystemRoot}\TEMP" | Where { $_.Name -like "AppxErrorReport_*.txt" } | Remove-Item -Force - Get-ChildItem -Path "${Env:SystemRoot}\TEMP" | Where { $_.Name -like "AppXPackaging_*.evtx" } | Remove-Item -Force + 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 -Write-Output "Deleting old CBS log files from ${Env:SystemRoot}\Logs\CBS" -Get-ChildItem -Path "${Env:SystemRoom}\Logs\CBS" -File | Where { ( $_.Name -like "CbsPersist_*.log" ) -or ( $_.Name -like "CbsPersist_*.cab" ) } | Remove-Item -Force +$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 ${Env:SystemRoom}\TEMP" -Foreach ( $item in (Get-ChildItem -Path "${Env:SystemRoot}\TEMP") ) +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 }