Ask a questionAsk a question
 

AnswerEmpty recycle bin - confused

  • Tuesday, October 20, 2009 7:29 AMbr4tt3 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi all,

    I have a got problem, I want to be able to script the possibility to conserve space by deleting items in the Recycle Bin (under Windows 7 & Windows 2008R2). I checked the entries several people posted in different forums on how to do this with PowerShell, and came up with the following answer:

    #Example:

    $RecycleBin = 0xA
    $objShell = New-Object -ComObject "Shell.Application"
    $objFolder = $objShell.Namespace($RecycleBin)
    $objFolderItem = $objFolder.Self
    $objFolderItem.InvokeVerbEx("Empty Recycle &Bin")

    #End Example!

    My problem is, it does NOT seem to work! I want to do it under Windows 7 and 2008 R2, using PowerShell 2.0 - I can get the COM object to work, check out the properties ($objFolderItem = $objFolder.Self) and for example verify the verbs that can be executed, for example (Empty Recycle &Bin). People also refer to that you are supposed to confirm the deletion, but for me there is no confirmation being displayed, and I cant seem to find how to specify the cofirmation parameter to the COM object on MSDN, all I can see is that the declaration: InvokeVerbEx InvokeVerbEx (Variant, Variant) <-- Variant?

    There is NO errors being displayed when executing the code above, it just works from the command line, however, there is deletion taking place from the Recycler which makes me wonder.... pointers, tips? anyone?

    Thanks....
    br4tt3

Answers

  • Wednesday, November 04, 2009 5:52 PMMike Pfeiffer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    This will delete everything in the recycle bin with no confirmation:

    $objShell = New-Object -Com Shell.Application
    $objFolder = $objShell.Namespace(0xA)
    $objFolder.items() | %{ remove-item $_.path -Recurse -Confirm:$false}
    • Proposed As Answer byMike Pfeiffer Wednesday, November 04, 2009 5:52 PM
    • Marked As Answer bybr4tt3 Thursday, November 05, 2009 8:28 AM
    •  

All Replies

  • Tuesday, October 20, 2009 8:13 PMTom Lavedas Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am probably going to get myself in trouble here, since I really don't know PowerShell.  However, I have used Shell.Application and a quick translation into WSH/VBS worked perfectly the first time in XP.  Therefore, I am going to guess that the verb you are trying to invoke is NOT rendered as "Empty Recycle &Bin" as you expect.  Rather, there is either an OS or local language difference that has another verb for emptying the Recycle bin.

    You might try running a foreach loop to show you exactly what the verbs are and find the one that is used for the Recycle bin.

    $RecycleBin = 0xA
    $objShell = New-Object -ComObject "Shell.Application"
    $objFolder = $objShell.Namespace($RecycleBin)
    $objFolderItem = $objFolder.Self
    foreach ($verb in get-childitem $objFolderItem.verbs) {$verb.name}

    If I have read the PS Help correctly, that should display all of the verbs associated with the Recycle bin.  An examination of the results might give you a clue as to what is wrong.

    BTW, when this verb IS correctly invoked, it should result in a Yes/No GUI confirmation dialog box popup.  I don't know how that might be suppressed, if it is necessary to do that.

    Tom Lavedas
    • Edited byTom Lavedas Tuesday, October 20, 2009 8:15 PMAdded a missing $
    •  
  • Wednesday, November 04, 2009 9:40 AMbr4tt3 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi and thanks for the feedback, running your code in PowerShell, I modified as below: (which returns all the verbs that can be used)

    $RecycleBin = 0xA
    $objShell = New-Object -ComObject "Shell.Application"
    $objFolder = $objShell.Namespace($RecycleBin)
    $objFolderItem = $objFolder.Self
    foreach ($verb in $objFolderItem.verbs()) {$verb.name}

    Below is output:

    &Open
    Empty Recycle &Bin
    Create &shortcut
    Rena&me
    P&roperties

    Still based on this finding, copy & pasting and then executing:

    $objShell = New-Object -Com Shell.Application
    #Special folder: (0xA) - RecycleBin
    $objFolder = $objShell.Namespace(0xA)
    $objFolderItem = $objFolder.Self
    $objFolderItem.InvokeVerbEx("Empty Recycle &Bin")

    Gives me no errors, accepts command, no effect, no confirmation GUI dialog box or anything? I dont know what I am missing here but sure would like to know!





    br4tt3
  • Wednesday, November 04, 2009 5:52 PMMike Pfeiffer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    This will delete everything in the recycle bin with no confirmation:

    $objShell = New-Object -Com Shell.Application
    $objFolder = $objShell.Namespace(0xA)
    $objFolder.items() | %{ remove-item $_.path -Recurse -Confirm:$false}
    • Proposed As Answer byMike Pfeiffer Wednesday, November 04, 2009 5:52 PM
    • Marked As Answer bybr4tt3 Thursday, November 05, 2009 8:28 AM
    •  
  • Thursday, November 05, 2009 8:30 AMbr4tt3 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for the advice, worked out well!

    /Br4tt3
    br4tt3