Sunday, September 26, 2010

Powershell browse and recycle functions

I just added browse and recycle functions to my powershell profile: Documents/WindowsPowershell/profile.ps1. The browse function opens up an explorer window at a specified path, and the recycle function sends a path to the recycle-bin after prompting for user confirmation.

# Excerpt from profile.ps1
...
$GLOBAL:docs = $home + "\Documents"
$GLOBAL:trash = $Env:TEMP
$GLOBAL:shell = new-object -com Shell.Application
$Env:nodosfilewarning=1
...
function recycle( $path ) {
    $clean = resolve-path( $path )
    $item = $shell.Namespace(0).ParseName( $clean )
    $item.InvokeVerb("delete") 
}

function browse( $path ) {
    $clean = resolve-path( $path )
    #echo $clean.path
    $shell.open( $clean.path ) 
}
Post a Comment