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

后端 未结 7 971
攒了一身酷
攒了一身酷 2020-12-07 20:19

By default when you delete a file using PowerShell it\'s permanently deleted.

I would like to actually have the deleted item go to the recycle bin just like I would

相关标签:
7条回答
  • 2020-12-07 21:18

    Here is a complete solution that can be added to your user profile to make 'rm' send files to the Recycle Bin. In my limited testing, it handles relative paths better than the previous solutions.

    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
    
    0 讨论(0)
提交回复
热议问题