PowerShell清空回收站 2


在旧的PowerShell版本中,如果要清空回收站,需要用户手动调用Window com 对象的接口,可以参考之前的文章《PowerShell将文件删除进回收站》。但是在Powershell 5.0 中 可以直接使用命令Clear-RecycleBin命令:

比如强制清空c盘的回收站:

Clear-RecycleBin -Force -DriveLetter c

比如我想强制清空所有回收站

Get-PSDrive -PSProvider FileSystem | where {
Clear-RecycleBin -Force -DriveLetter $_.Name 
}

但是上面的脚本有问题,因为如果你的机器上有一个DVD光驱,会提示出错:

Clear-RecycleBin : The drive with the name ‘E:\’ is not a Fixed drive and does not
support the Recyle Bin. Please run the ‘Get-Volume’ cmdlet to see the available Fixed
drives in the system.

基于上面的提示,通过Get-Volume过滤,将脚本改成下面的样子,暂时妥妥了。

Get-Volume | Where-Object { 
($_.DriveType -eq 'Fixed') -and ($_.DriveLetter -ne $null) } | 
foreach { 
Clear-RecycleBin -Force -DriveLetter $_.DriveLetter 
}
本文链接: https://www.pstips.net/clear-recyclebin.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

2 条评论 “PowerShell清空回收站