PowerShell清理臨時文件夾,當磁盤空間變小時,我們可以通過清理臨時文件夾來解決。我們使用PowerShell來寫一個程序,寫可以清理Windows臨時文件夾中30天以前的文件。
$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)