$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 -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 } ## 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 ## 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 -ErrorAction SilentlyContinue } ## Remove all system temp files older than the specified number of days $OlderThan=7 $timedelta = New-TimeSpan -Days $OlderThan Write-Output "Deleting all files more than ${OlderThan} day(s) old ${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 -ErrorAction SilentlyContinue } }