在旧的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
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
博主很认真的样子。
大家先在自己PS环境下运行这个命令,看看自己的PS版本:$PSVersionTable
版本不是5的,洗洗睡吧。