Here I will show you how to perform different operations on SharePoint Site Recycle bin using PowerShell Scripts.
1. Get the Site Collection Recycle bin Item count :
- #check to see if the PowerShell Snapin is added
- if ( (Get-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null )
- {
- Add-PsSnapin "Microsoft.SharePoint.PowerShell"
- }
- $siteUrl = Read-Host "Enter the url of the Site Collection :"
- $SPSite = Get-sPSite $siteUrl
-
- #Contains both First Stage & Second Stage Recycle bin Items
- $SPRecycleBinItemCollection = $SPSite.RecycleBin;
-
- write-host "Processing Site: " $SPSite.RootWeb.Title "`n"
- write-host "Total No. of Items in Recycle bin: " $SPRecycleBinItemCollection.Count
2. Restore All Site Collection Recycle Bin items :
- If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
- { Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
-
- #Definition of the function that restores de deleted Site Collection
- function WorkWithSPRecycleBin
- {
- $recyclebinitems = (Get-SPWeb “<Site Collection URL>” ).RecycleBin
- foreach($recyclebinitem in $recyclebinitems)
- {
- (Get-SPWeb “<Site Collection URL>”).RecycleBin.Restore($recyclebinitem.ID)
- }
- }
-
- Start-SPAssignment –Global
- #Calling the function
- WorkWithSPRecycleBin
- Stop-SPAssignment –Global
-
- Remove-PSSnapin Microsoft.SharePoint.PowerShell
3. Delete all SharePoint Site Recycle Bin items :
- If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
- { Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
-
- #Definition of the function that restores de deleted Site Collection
- function WorkWithSPRecycleBin
- {
- $recyclebinitems = (Get-SPWeb “<Site Collection URL>” ).RecycleBin
- foreach($recyclebinitem in $recyclebinitems)
- {
- (Get-SPWeb “<Site Collection URL>”).RecycleBin.DeleteAll();
- }
- }
-
- Start-SPAssignment –Global
- #Calling the function
- WorkWithSPRecycleBin
- Stop-SPAssignment –Global
-
- Remove-PSSnapin Microsoft.SharePoint.PowerShell