Thursday, 10 May 2018

Sharepoint Recycle Bin

SharePoint web application using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$WebApp=Get-SPWebApplication "http://test.com"
foreach ($SPSite in $webApp.Sites)
{
   foreach($SPWeb in $SPSite.AllWebs)
   {
             $SPWeb.RecycleBin.MoveAllToSecondStage();
              write-host "End-User Recycle Bin Items Deleted for:"
              write-host $SPWeb.title ":" $SPWeb.URL "`n"
              $SPWeb.Dispose()
    }
             $SPSite.RecycleBin.DeleteAll();
             $SPSite.Dispose()
             write-host "Administrator Recycle bin Items Deleted for:" $SPSite.RootWeb.title "`n"
}




SharePoint Site Collection using PowerShell:


$Site = Get-SPSite "https://test.com/sites/test"
$Site.AllWebs | Foreach-object { $_.RecycleBin.MoveAllToSecondStage() }
$Site.RecycleBin.DeleteAll();


Restore based on Object Type (such as List, Web, ListItem, etc):


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SiteURL="http://test.com"
$site = Get-SPSite $SiteURL
$RecycleBin = $site.RecycleBin
$DeletedItems = $RecycleBin | Where{ $_.ItemType -eq "List"}
if($DeletedItems)
{
 Foreach($Item in $DeletedItems)
 {
  $Item.Restore()
  Write-Host "'$($Item.Title)' Restored from Recycle Bin!" -f DarkGreen
 }
}


Restore all items from SharePoint Recycle bin:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SiteURL="http://test.com/sites/test"
$Site = Get-SPSite $SiteURL
$DeletedItems = $Site.RecycleBin
if($DeletedItems)
{
 foreach($Item in $DeletedItems)
 {
  $Site.RecycleBin.restore($Item.ID)
  Write-Host "Item restored:"$Item.Title
 }
}

No comments:

Post a Comment