Tuesday, July 9, 2013

How to use the Recycle Bin (Empty/Restore) on Free Core Server 2012

This has now been added to pshvm.codeplex.com a free powershell hyper-v manager project.

UPDATE: it has been brought to my attention by Vincent Loosveld that if you are using the freeware app A43 as the file manager and shortcuts launcher as I have created it in PSHVM then you can easily GUI access the recycle bin see pic below.


But if you want to do it with powershell here is how:
Here is the code: File named get-recycled.ps1

<#
.Deleting items more than 10 days old
Get-Recycled | ?{$_.Age.TotalDays -gt 10} | Remove-Item

#Get original location of all items deleted in the last hour
Get-Recycled | ?{$_.Age.TotalHours -le 1} | %{$_.OriginalFullName}

.Restore all zip files without overwriting newer files with the same names.
Get-Recycled | ?{$_.Extension -eq ".zip"} | %{Move-Item $_ $_.OriginalFullName}

.Restore all zip files, overwriting newer files if necessary.
Get-Recycled | ?{$_.Extension -eq ".zip"} | %{Move-Item $_ $_.OriginalFullName -Force}
#>

function get-recycled
{
$sa = New-Object -ComObject Shell.Application
$DisplaceDateId = '{9B174B33-40FF-11D2-A27E-00C04FC30871}3'
$DisplaceFromId = '{9B174B33-40FF-11D2-A27E-00C04FC30871}2'
$sa.Namespace(10).Items() | %{
    $item = Get-Item -LiteralPath $_.path;
    $OriginalName = $_.Name
    $whenUtc = $_.ExtendedProperty($DisplaceDateId);
    $whenLocal = $whenUtc.ToLocalTime();
    $OriginalParent = [IO.DirectoryInfo]($_.ExtendedProperty(
        $DisplaceFromId))
    $OriginalFullName = Join-Path $OriginalParent $OriginalName
    Add-Member -MemberType NoteProperty -Name DeletionTimeUtc `
        -Value ($whenUtc) -InputObject $item;
    Add-Member -MemberType NoteProperty -Name DeletionTime `
        -Value ($whenLocal) -InputObject $item;
    Add-Member -MemberType ScriptProperty -Name Age `
        -Value {(Get-Date) - $this.DeletionTime} `
        -InputObject $item;
    Add-Member -MemberType NoteProperty -Name OriginalName `
        -Value ($OriginalName) -InputObject $item;
    Add-Member -MemberType NoteProperty -Name OriginalParent `
        -Value $OriginalParent -InputObject $item;
    Add-Member -MemberType NoteProperty -Name OriginalFullName `
        -Value $OriginalFullName -PassThru -InputObject $item;
}
}
$list = Get-Recycled
cls
$list

#Ask for action
write-host ""
write-host "If you do not see any files listed above then there are no deleted files"
write-host ""
Write-host -foreground "magenta" "0. Exit this Script"
Write-host -foreground "magenta" "1. Empty Recycle Bin"
Write-host -foreground "magenta" "2. Restore a ingle file"
Write-host -foreground "magenta" "3. Restore all files with Extension (ex .txt)"
$SelectedIndex4 = Read-Host "What do you want to do?"
$Selection = $SelectedIndex4

if($Selection -eq 0){exit}

if($Selection -eq 1){
Get-Recycled | Remove-Item
pause}

if($Selection -eq 2){
$SelectedIndex1 = Read-Host "Type the file name from what is listed above"
$Selection1 = $SelectedIndex1
Get-Recycled | ?{$_.Name -eq "$Selection1"} | %{Move-Item $_ $_.OriginalFullName}
pause}

if($Selection -eq 3){
$SelectedIndex2 = Read-Host "Type the file Extension to restore (restores all with that Ext)"
$Selection2 = $SelectedIndex2
Get-Recycled | ?{$_.Extension -eq ".$Selection2"} | %{Move-Item $_ $_.OriginalFullName}
pause}


NOTE: this will recover the file but not the file extension..not sure why. So you will need to manually add the file extension back onto the file name.

No comments:

Post a Comment