如何实现将文件删除到回收站而不是永久删除?

How can I implement Deleting files to the Recycle bin rather than Permanently deleting?

我有一台服务器可以继续缩小我们 D: 存储空间的差距。因此,有时我会进入该位置并手动删除文件以释放一些 space "Yuck"。我创建了一个简单的 Powershell 脚本,允许我专门配置哪些文件以及需要删除的日期(这很重要)。不幸的是,在测试期间,我很快意识到这些文件已被永久删除,我宁愿将它们发送到垃圾箱。

代码如下:

   Get-ChildItem -Recurse C:\Temp\TWLogTest\*.* | Where-Object {$_.CreationTime -gt (get-date "12-03- 2016 01:00AM") -and $_.
   CreationTime -lt (get-date "10-10-2019 14:00PM")} | Where-Object {$_.name -match "tomcat"} | Remove-Items

任何有关在此特定脚本中实施删除的帮助、指导或想法,我们将不胜感激。我看过 Module-Recycle,但仍然不确定如何在此处添加它。

谢谢, 马特

基于此处:How do I move a file to the Recycle Bin using PowerShell?

using assembly microsoft.visualbasic
using namespace microsoft.visualbasic

[FileIO.FileSystem]::DeleteFile('foo.txt', 'OnlyErrorDialogs', 'SendToRecycleBin')

类似这样的内容应该与您的代码相匹配。该方法仅适用于文件。比较日期时,第二个参数无论如何都会转换为 [datetime]。

using assembly microsoft.visualbasic
using namespace microsoft.visualbasic

Dir -File -R C:\Temp\TWLogTest | Where { 
  $_.CreationTime -gt '12/3/16 1AM' -and 
  $_.CreationTime -lt '10/10/19 2PM' -and 
  $_.name -match 'tomcat' } | 
foreach { [FileIO.FileSystem]::DeleteFile($_.fullname, 'OnlyErrorDialogs',
  'SendToRecycleBin') }