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 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.
