PowerShell脚本清理指定天数前的临时文件夹实现代码_PowerShell教程-查字典教程网
PowerShell脚本清理指定天数前的临时文件夹实现代码
PowerShell脚本清理指定天数前的临时文件夹实现代码
发布时间:2016-12-28 来源:查字典编辑
摘要:PowerShell清理临时文件夹,当磁盘空间变小时,我们可以通过清理临时文件夹来解决。我们使用PowerShell来写一个程序,写可以清理...

PowerShell清理临时文件夹,当磁盘空间变小时,我们可以通过清理临时文件夹来解决。我们使用PowerShell来写一个程序,写可以清理Windows临时文件夹中30天以前的文件。

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)

说明:

1、Windows临时目录在PowerShell中可以使用$env:temp来表示。

2、通过一个$cutoff变量来控制要删除文件的时间刻度,30天这个参数可以定制。

3、最后计算了一下释放了多少空间

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新PowerShell学习
    热门PowerShell学习
    脚本专栏子分类