PowerShell脚本清理指定天数前的临时文件夹实现代码
PowerShell清理临时文件夹,当磁盘空间变小时,我们可以通过清理临时文件夹来解决。我们使用PowerShell来写一个程序,写可以清理Windows临时文件夹中30天以前的文件。
PowerShell清理临时文件夹
当磁盘空间变小时,我们可以通过清理临时文件夹来解决。我们使用PowerShell来写一个程序,写可以清理Windows临时文件夹中30天以前的文件。
$cutoff=(Get-Date)-(New-TimeSpan-Days30) $before=(Get-ChildItem$env:temp|Measure-ObjectLength-Sum).Sum
Get-ChildItem$env:temp| Where-Object{$_.Length-ne$null}| Where-Object{$_.LastWriteTime-lt$cutoff}| Remove-Item-Force-ErrorActionSilentlyContinue-Recurse-WhatIf $after=(Get-ChildItem$env:temp|Measure-ObjectLength-Sum).Sum $freed=$before-$after 'Cleanupfreed{0:0.0}MB.'-f($freed/1MB)