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)