I need a powershell command which deletes the second stage recyclebin and the possibilities are stated below
Possibilities :
->If it is possible to get a script wherein if we give an user name for ex Deleted By “THEJA,Mr.RAVURI(THEJA)”,it should delete the all the files which were deleted by user.
->Another possibility is get the files based on Deleted which is like, the script should delete all the files which were deleted on specific date.
->to delete all the files which are present in the Admin recycle bin.
->to delete first “n” number of files which are present in recyclebin with ascending or descending order.
Many thanks in Advance and let me know if you require any clarifications on this
Theja.
Hi Ajeet,
I got this error when i tried this script:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$WebApp=get-spwebapplication “http://site”
foreach ($SPSite in $webApp.Sites)
{
#get the collection of webs
write-host “End-User Recycle Bin Items Deleted for:”
write-host $SPWeb.title “:” $SPWeb.URL “`n”
}
#Empty SharePoint site collection recycle bin (Second Stage Recycle bin) or Admin Recycle bin
$SPSite.RecycleBin.DeleteAll();
write-host “Administrator Recycle bin Items Deleted for:” $SPSite.RootWeb.title “`n”
}
Get-SPWebApplication : Cannot find an SPWebApplication object with Name, Id, or Url: https://test.sharepoint..intranet.com/sitebasic
At C:\users\theravu-a\Desktop\New Text Document.ps1:3 char:29
+ $WebApp=get-spwebapplication <<<< “https://test.sharepoint..intranet.com/sitebasic“
+ CategoryInfo : InvalidData: (Microsoft.Share…tWebApplication:SpCmdletGetWebApplication) [Get-SPWebAppli
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SpCmdletGetWebApplication
You cannot call a method on a null-valued expression.
At C:\users\theravu-a\Desktop\New Text Document.ps1:8 char:29
+ $SPSite.RecycleBin.DeleteAll <<<< ();
+ CategoryInfo : InvalidOperation: (DeleteAll:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any reason for this error.
Hi  Theja,
If you will see the above PowerShell command, you will find that I have described that, If you want to remove entries from 2nd level recycle bin, Then you can just skip the iteration over web foreach ($SPSite in $webApp.Sites)…..
Rest will work as you want.
Please let me know if it is fine. Else I will provide you exact PowerShell Cmdlet.
Hi Ajeet,
Many thanks for the reply.Sorry for not giving more details.
The script which you have provided me is applies for entire web application(to my knowledge).I need this script to be run for a dedicated site collection like “https://sharepoint.test.india.com/sitebasic”.It would be very much helpful for me if you could provide the same for targeting dedicated site collection.
Many thanks for your time and help.
Theja.
Hi Theja,
Try this:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$WebApp=get-spwebapplication “http://site”
foreach ($SPSite in $webApp.Sites)
{
#get the collection of webs
foreach($SPWeb in $SPSite.AllWebs)
{
#Empty the 1st Stage Recycle bin items PERMENANTLY
#$SPWeb.RecycleBin.DeleteAll();#Send the 1st Stage Recycle bin items to 2nd Stage
$SPWeb.RecycleBin.MoveAllToSecondStage();write-host “End-User Recycle Bin Items Deleted for:”
write-host $SPWeb.title “:” $SPWeb.URL “`n”
}
#Empty SharePoint site collection recycle bin (Second Stage Recycle bin) or Admin Recycle bin
$SPSite.RecycleBin.DeleteAll();write-host “Administrator Recycle bin Items Deleted for:” $SPSite.RootWeb.title “`n”
}
It will also able to move 1st stage recycle bin values to 2nd stage and then it will delete all the entries from 2nd stage recycle bin.
If you don’t want to move entries from 1st stage recycle bin then just ignore following block:
foreach($SPWeb in $SPSite.AllWebs)
{
#Empty the 1st Stage Recycle bin items PERMENANTLY
#$SPWeb.RecycleBin.DeleteAll();#Send the 1st Stage Recycle bin items to 2nd Stage
$SPWeb.RecycleBin.MoveAllToSecondStage();write-host “End-User Recycle Bin Items Deleted for:”
write-host $SPWeb.title “:” $SPWeb.URL “`n”
}
If you want to traverse your recycle bin you can use follwoing:
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
#Get the Site collection
$site = new-object Microsoft.SharePoint.SPSite(“http://site”)#Create new object for SPRecycleBinQuery
$query = new-object Microsoft.SharePoint.SPRecycleBinQuery#$query.ItemState = “FirstStageRecycleBin”
$query.ItemState = “SecondStageRecycleBin”#How many Rows to be returned
$query.RowLimit = 100#Call GetRecycleBinItems to Search Inside SharePoint Recycle Bin
$DeletedItemsColl = $site.GetRecycleBinItems($query)#Filter Result
$Result= $DeletedItemsColl | where {$_.Title -match “.txt”}#Write output
$Result | foreach-object {
(Write-Host “Found Item:” $_.Title)
(write-host “Created by:” $_.AuthorName)
(write-host “Deleted by:” $_.DeletedByName)
(write-host “Deleted Date:”$_.DeletedDate)
(write-host “File Size(Bytes):” $_.Size)
(write-host “Original Location:” $_.DirName)
(write-host “Web:” $_.web.URL)
(Write-Host “————————————“)
}
Â
Means you can delete your entries by using SPRecycleBinQuery also.