如何使用PowerShell将文件移动到回收站?

时间:2009-02-02 01:47:04

标签: powershell

默认情况下,使用PowerShell删除文件时,它会被永久删除。

我想将删除的项目实际上转到回收站,就像我通过shell删除一样。

如何在文件对象的PowerShell中执行此操作?

8 个答案:

答案 0 :(得分:27)

如果您不想总是看到确认提示,请使用以下内容:

Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('d:\foo.txt','OnlyErrorDialogs','SendToRecycleBin')

(解决方案由Shay Levy提供)

答案 1 :(得分:18)

它在PowerShell中的工作方式与Chris Ballance在JScript中的解决方案非常相似:

 $shell = new-object -comobject "Shell.Application"
 $folder = $shell.Namespace("<path to file>")
 $item = $folder.ParseName("<name of file>")
 $item.InvokeVerb("delete")

答案 2 :(得分:17)

这是一个缩短版本的简短版本

$path = "<path to file>"
$shell = new-object -comobject "Shell.Application"
$item = $shell.Namespace(0).ParseName("$path")
$item.InvokeVerb("delete")

答案 3 :(得分:12)

2017回答:使用Recycle module

Install-Module -Name Recycle

然后运行:

Remove-ItemSafely file

我想为此创建一个名为trash的别名。

答案 4 :(得分:9)

这是一个改进的函数,它支持目录和文件作为输入:

Add-Type -AssemblyName Microsoft.VisualBasic

function Remove-Item-ToRecycleBin($Path) {
    $item = Get-Item -Path $Path -ErrorAction SilentlyContinue
    if ($item -eq $null)
    {
        Write-Error("'{0}' not found" -f $Path)
    }
    else
    {
        $fullpath=$item.FullName
        Write-Verbose ("Moving '{0}' to the Recycle Bin" -f $fullpath)
        if (Test-Path -Path $fullpath -PathType Container)
        {
            [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
        }
        else
        {
            [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
        }
    }
}

答案 5 :(得分:2)

将文件移至RecycleBin:

description

将文件夹移至RecycleBin:

Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('e:\test\test.txt','OnlyErrorDialogs','SendToRecycleBin')

答案 6 :(得分:0)

以下是一个完整的解决方案,可以将其添加到您的用户个人资料中,以便制作出来。将文件发送到回收站。在我的有限测试中,它比以前的解决方案更好地处理相对路径。

Add-Type -AssemblyName Microsoft.VisualBasic

function Remove-Item-toRecycle($item) {
    Get-Item -Path $item | %{ $fullpath = $_.FullName}
    [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
}

Set-Alias rm Remove-Item-toRecycle -Option AllScope

答案 7 :(得分:0)

这是对 sba923s 的好答案的轻微修改。

我改变了一些东西,比如参数传递,并添加了一个 -WhatIf 来测试文件或目录的删除。

function Remove-ItemToRecycleBin {

  Param
  (
    [Parameter(Mandatory = $true, HelpMessage = 'Directory path of file path for deletion.')]
    [String]$LiteralPath,
    [Parameter(Mandatory = $false, HelpMessage = 'Switch for allowing the user to test the deletion first.')]
    [Switch]$WhatIf
    )

  Add-Type -AssemblyName Microsoft.VisualBasic
  $item = Get-Item -LiteralPath $LiteralPath -ErrorAction SilentlyContinue

  if ($item -eq $null) {
    Write-Error("'{0}' not found" -f $LiteralPath)
  }
  else {
    $fullpath = $item.FullName

    if (Test-Path -LiteralPath $fullpath -PathType Container) {
      if (!$WhatIf) {
        Write-Verbose ("Moving '{0}' folder to the Recycle Bin" -f $fullpath)
        [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
      }
      else {
        Write-Host "Testing deletion of folder: $fullpath"
      }
    }
    else {
      if (!$WhatIf) {
        Write-Verbose ("Moving '{0}' file to the Recycle Bin" -f $fullpath)
        [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
      }
      else {
        Write-Host "Testing deletion of file: $fullpath"
      }
    }
  }

}

$tempFile = [Environment]::GetFolderPath("Desktop") + "\deletion test.txt"
"stuff" | Out-File -FilePath $tempFile

$fileToDelete = $tempFile

Start-Sleep -Seconds 2 # Just here for you to see the file getting created before deletion.

# Tests the deletion of the folder or directory.
Remove-ItemToRecycleBin -WhatIf -LiteralPath $fileToDelete
# PS> Testing deletion of file: C:\Users\username\Desktop\deletion test.txt

# Actually deletes the file or directory.
# Remove-ItemToRecycleBin -LiteralPath $fileToDelete