|
Add-PSSnapin Microsoft.SharePoint.PowerShell |
|
|
|
#Variables |
|
$i = 0 |
|
|
|
#SharePoint Site Collection URL |
|
$url = "https://sharepoint.jcallaghan.com" |
|
|
|
#$url = Read-Host "Enter a valid URL to a SharePoint Site Collection?" |
|
#if($url -eq ""){write-host "No URL provided." -foregroundcolor Red; Exit} |
|
|
|
#How many days ago should items be deleted from? |
|
$deleteFrom = -10 |
|
|
|
#$deleteFrom = Read-Host "Remove items older than how many days?" |
|
#if($deleteFrom -eq ""){write-host "No value provided." -foregroundcolor Red; Exit} |
|
|
|
#Create report in script path |
|
$scriptpath = $MyInvocation.MyCommand.Path |
|
$dir = Split-Path $scriptpath |
|
$report = "$($dir)\DeletedSecondStateRecycleBinItems.csv" |
|
|
|
#Date calculations |
|
$dateNow = Get-Date |
|
$dateDiff = $dateNow.AddMinutes(-$deleteFrom) |
|
#$dateDiff = $dateNow.AddDays($deleteFrom) |
|
|
|
#Display date/times for review in table |
|
$table = @() |
|
$review = New-Object System.Object |
|
$review | Add-Member -type NoteProperty -Name "Date" -Value "Timestamp now" |
|
$review | Add-Member -type NoteProperty -Name "Value" -Value $dateNow |
|
$table += $review |
|
$review = New-Object System.Object |
|
$review | Add-Member -type NoteProperty -Name "Date" -Value "Files older than" |
|
$review | Add-Member -type NoteProperty -Name "Value" -Value $dateDiff |
|
$table += $review |
|
$table | Format-Table –AutoSize |
|
|
|
#Connect to the site |
|
$site = Get-SPsite $url |
|
|
|
#Report file and first row |
|
New-Item $report -type file -Force | Out-Null |
|
Add-Content $report "Deleted Items: $($dateNow)" |
|
Add-Content $report "Name, Title, Deleted by, Deleted date, Path, File Guid" |
|
|
|
#Get items from the Seconday Stage Recycle Bin (SSRB) that are older than are removel period. |
|
$items = $site.Recyclebin | where { $_.ItemState -eq "SecondStageRecycleBin" -and $_.deleteddate -le $dateDiff} |
|
$site.Recyclebin | where { $_.ItemState -eq "SecondStageRecycleBin" -and $_.deleteddate -le $dateDiff} | Format-Table -Property Title, Web, DeletedBy, DeletedDate -Autosize -Wrap |
|
|
|
#Confirm there are items to delete |
|
if($items -ne $null){ |
|
|
|
#Create prompt |
|
$ok = New-Object System.Management.Automation.Host.ChoiceDescription "&OK","Description." |
|
$cancel = New-Object System.Management.Automation.Host.ChoiceDescription "&CANCEL","Description." |
|
$options = [System.Management.Automation.Host.ChoiceDescription[]]($ok, $cancel) |
|
$title = "Confirm"; $message = "Delete items from Second Stage Recycle Bin?" |
|
$result = $host.ui.PromptForChoice($title, $message, $options, 1) |
|
|
|
switch ($result) { |
|
0{ |
|
#Get items to be deleted |
|
$site.Recyclebin | where { $_.ItemState -eq "SecondStageRecycleBin" -and $_.deleteddate -le $dateDiff} | foreach{ |
|
#Add entry to report |
|
Add-Content $report "$($_.LeafName),$($_.Title),$($_.deletedbyname),$($_.deleteddate),$($_.Dirname),$($_.Id)" |
|
#Delete item by ID |
|
$site.Recyclebin.Delete($_.ID) |
|
$i++ |
|
} |
|
write-host "$($i) items removed from Second Stage Recycle Bin." |
|
}1{ |
|
write-host "Cancelled by user." -foregroundcolor Red |
|
} |
|
} |
|
}else{ |
|
write-host "No files were found in the Second Stage Recycle Bin." -foregroundcolor Red |
|
} |
|
|
|
#Dispose |
|
$site.dispose(); |