tags:

views:

679

answers:

4

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 have happen via a shell delete.

How can you do this in PowerShell on a file object?

A: 

JScript:

function main()
{
var Shell =3D WScript.CreateObject("Shell.Application");
var Folder =3D Shell.Namespace("<path to file>");
var Item =3D Folder.ParseName("<name of file>");
Item.InvokeVerb("delete");
}

http://www.eggheadcafe.com/software/aspnet/30890097/send-file-to-recycle-bin.aspx

Chris Ballance
+7  A: 

It works in PowerShell pretty much the same way as Chris Ballance's solution in JScript:

 $shell = new-object -comobject "Shell.Application"
 $folder = $shell.Namespace("<path to file>")
 $item = $folder.ParseName("<name of file>")
 $item.InvokeVerb("delete")
Alexander Shirshov
There is a small bug in your answer (but it did work!).You need a quote before "path to file"$folder = $shell.Namespace(<path to file>")becomes$folder = $shell.Namespace("<path to file>")
Omar Shahine
+1  A: 

Here is a shorter version that reduces a bit of work

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

The solution above requires a confirmation for each file. Is there an alternative?

Medo