簡體   English   中英

如何使用 PowerShell 將文件移動到回收站?

[英]How do I move a file to the Recycle Bin using PowerShell?

使用rm命令刪除Powershell中的文件時,永久刪除。

而不是這個,我希望將已刪除的項目 go 放入回收站,就像通過 UI 刪除文件時發生的情況一樣。

你怎么能在 PowerShell 中做到這一點?

如果您不想總是看到確認提示,請使用以下命令:

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

(謝伊利維提供的解決方案)

2017 答案:使用回收模塊

Install-Module -Name Recycle

然后運行:

Remove-ItemSafely file

我喜歡為此創建一個名為trash的別名。

它在 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")

這是一個較短的版本,可以減少一些工作

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

這是一個改進的 function,它支持目錄和文件作為輸入:

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')
        }
    }
}

刪除文件到回收站:

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

刪除文件夾到回收站:

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

這是一個完整的解決方案,可以添加到您的用戶配置文件中,以使“rm”將文件發送到回收站。 在我有限的測試中,它比以前的解決方案更好地處理相對路徑。

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

這是 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM