PowerShell cleans up temporary folders, and when disk space changes, we can resolve them by cleaning up the Temp folder. We use PowerShell to write a program that can clean up files that were 30 days old in the Windows Temp folder.
PowerShell Clean up temporary folders
When disk space changes to an hour, we can resolve it by cleaning up the temporary folder. We use PowerShell to write a program that can clean up files that were 30 days old in the Windows Temp folder.
Copy Code code as follows:
$cutoff = (get-date)-(new-timespan-days 30)
$before = (get-childitem $env: Temp | Measure-object length-sum). Sum
Get-childitem $env: Temp |
Where-object {$_. Length-ne $null} |
Where-object {$_. Lastwritetime-lt $cutoff} |
Remove-item-force-erroraction silentlycontinue-recurse-whatif
$after = (get-childitem $env: Temp | Measure-object length-sum). Sum
$freed = $before-$after
' Cleanup freed {0:0.0} MB. '-F ($freed/1MB)
Description
1. The Windows Temp directory can be expressed in PowerShell using $env:temp.
2, through a $cutoff variable to control the time scale to delete files, 30 days This parameter can be customized.
3, the final calculation of how much space released