locked
How can I delete "to recycle bin" in powershell instead of remove item ? RRS feed

  • Question

  • Hi,

    After one of my scripts recently deleted some files I realized I wanted to keep, I would like to replace my normal "remove-item" with a "delete like in File Explorer which actually doesn't delete but sends to the recycle bin".

    I've done some digging but cannot find anything here, just lots of posts around SharePoint Recycle bins etc. 

    Many thanks!

    Gavin

    Tuesday, September 12, 2017 1:47 PM

All replies

  • Here's a google search, with many different ways

    https://www.google.com/search?q=powershell+send+to+recycle+bin&rlz=1C1GGRV_enUS749US749&oq=powershell+send+to+recycle+bin&aqs=chrome.0.0j69i57.9479j0j7&sourceid=chrome&ie=UTF-8


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Proposed as answer by I.T Delinquent Wednesday, September 13, 2017 9:58 AM
    Tuesday, September 12, 2017 2:37 PM
  • Hi Gavin,

    Based on the research and test in my lab, the following scripts could delete files to recycle bin rather than delete directly. For your reference, hope it is helpful to you:
    $paths = Get-ChildItem -Path 'D:\'
    $shell = New-Object -ComObject 'Shell.Application'
    
    ForEach ($path in $paths)
    {
    	$shell.NameSpace(0).ParseName($path.FullName).InvokeVerb('delete')
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, September 13, 2017 9:23 AM
  • Hi,

    Just checking in to see if the information provided was helpful. Does the scripts work?

    Please let us know if you would like further assistance.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, September 18, 2017 7:39 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, September 21, 2017 11:14 AM
  • Albert,

    I realise this is an old thread but I thought I'd thank you for this code.  I am using it successfully in batch files that call the PS procedure to do the deletion.

    The batch command allows single or multiple deletions to be called, as required, by calling the PS to do the job.  This line is inserted wherever required and tailored to suit.

    :: List the condemned files, using full paths, after the call to the ps1
    powershell.exe -ExecutionPolicy ByPass -File "C:\Tools\DeleteToRecycleBin.ps1"  "D:\Documents\IS\Scripting\PowerShell\Notes\delete\DeleteToRecycleBin - TechNet - Copy.pdf" "D:\Documents\IS\Scripting\PowerShell\Notes\delete\DeleteToRecycleBin - TechNet - Copy (2).pdf" 

    The PS file deletion written in C:\Tools\DeleteToRecycleBin.ps1

    ForEach ($argument in $args) {
        $paths = Get-ChildItem -Path "$argument"
        $shell = New-Object -ComObject 'Shell.Application'
            ForEach ($path in $paths)
            {
    	$shell.NameSpace(0).ParseName($path.FullName).InvokeVerb('delete')
            }
    }

    This therefore allows me to do what the OP asked about - delete files in batch file procedures yet retain the benefit of their being sent to the Recycle bin.

    I have not written a modified form of this for folder deletions i.e. for deletion of folders themselves rather than deletion of the contents of folders. 

    Denis





    Friday, November 23, 2018 8:39 PM
  • Function Send-ToRecycleBin
    {
        Param(
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true)]
        [alias('FullName')]
        [string]$FilePath
        )
        Begin{$shell = New-Object -ComObject 'Shell.Application'}
        Process{
            $Item = Get-Item $FilePath
    $shell.namespace(0).ParseName($item.FullName).InvokeVerb('delete')
        }
    }

    Why not use the pipeline for this?

    $FilePath = "c:\temp\NewFolder","c:\temp\SomeFile.txt"
    Get-ChildItem $FilePath | Send-ToRecycleBin
    ---Deletes the file or files in $FilePath (NewFolder and sub-folders, and SomeFile.txt)

    c:\temp\SomeFile.txt | Send-ToRecycleBin
    ---Deletes the file 'SomeFile.txt'

    c:\temp | Send-ToRecycleBin---Deletes the entire folder.


    Friday, January 17, 2020 8:44 PM