Working with SharePoint’s Second Stage Recycle Bin in PowerShell

I thought I’d share a PowerShell script that I’ve created to perform a few tasks against a Site Collection Second Stage Recycle Bin (SSRB) in SharePoint.

Remove-SecondStageRecycleBinItems.ps1
Remove-SecondStageRecycleBinItems.ps1

The requirement was to delete items that were older than a set number of days from the Second Stage Recycle Bin (SSRB). A record of each item deleted also needed to be added to a report.  But SharePoint can do this already I hear you say…well yes if a Site Collection quotas and the auditing features are used. In this scenario neither could be.

To display items in the Second State Recycle Bin in a table I used this command.

[code lang=”powershell”]$site.Recyclebin | where { $_.ItemState -eq "SecondStageRecycleBin" -and $_.deleteddate -le $dateDiff} | Format-Table -Property Title, Web, DeletedBy, DeletedDate -Autosize -Wrap
[/code]

Then to remove each item from the Recycle Bin I used the delete command.

[code lang=”powershell”]$site.Recyclebin.Delete($_.ID)[/code]

The full script is shared below. Remember to review, rename and test this script before using it in a production environment.

[code lang=”powershell”]
#——————————————————————————–
#
# Remove-SecondStateRecycleBinItems.ps1
#
# Author: James Callaghan (www.jcallaghan.com)
# Date: April 2014
#
# This script will delete items from the Second Stage Recycle Bin that
# are older than XX days.
#
#——————————————————————————–

Add-PSSnapin Microsoft.SharePoint.PowerShell

#Variables
$i = 0

#SharePoint Site Collection URL
$url = "http://apm.dev.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();
[/code]

One quirk I found while creating the script was that through the web browser, SharePoint reported the time each file was deleted correctly whereas in PowerShell, the time was not honouring GMT summer time.

British Summer Time  in SharePoint vs. PowerShell
British Summer Time in SharePoint vs. PowerShell

Enjoy and delete carefully!

All #SPC14 sessions available in a single spreadsheet!

Update: Wednesday, 8th January 2014. This post has seen an incredible amount of traffic which I have found to be a very rewarding experience- thank you! I’ve lived up to my word and managed to export the speaker information. Both the spreadsheet and PowerShell script have been updated to include this information.

Something I have found frustrating with the SharePoint Conference 2014 website over the holidays is that you have to browse through the sessions as search results pages. It makes planning how I want to fill my days at the conference very difficult.

Spreadsheet containing all the SharePoint Conference 2013 sessions
Spreadsheet containing all the SharePoint Conference 2014 sessions

I also wanted to sit down with my colleagues to see if there are any particular sessions that interest them. Without all the session being available in a format such as a spreadsheet this would become a very tiresome task.

There was no way I was going to do this by hand – at this time there is about 183 published sessions and 12 pieces of information per session that would require me to use copy and paste 4392 times and click between a browser and Excel 600 times…no thank you

Jumping the gun the a little maybe as I have yet to register (this is top of my to-do list when I’m back in the office on Monday and I’ll kick myself if this is available when you register!) but I broke out PowerShell and wrote a script to download all the information for the sessions from the SharePoint Conference (#SPC14) website to a spreadsheet – both of which you can download.

PowerShell script to export all the #SPC14 sessions to Excel
PowerShell script to export all the #SPC14 sessions to Excel

A spreadsheet of all the SPC14 sessions can be downloaded with this link and the script is available here.

By no means is this script particularly complex or elegant – but I really wanted this information in a spreadsheet and pretty fast so forgive me if it is not up to my usual standard…the key thing is I achieved what I set out to do and can share it with you all. The last piece of information which I’m still trying to export are the speakers for the sessions – I’ll update if I manage it.

I hope you find the #SPC14 session spreadsheet helpful – see you at the conference!

Security trimmed top navigation links

I was asked to review a client environment yesterday to find out why the links in their top navigation bar were displaying for users that did not have permission to the particular sites.

Creating sites

It turns out that when sites were being created by the client on SharePoint Foundation 2010 they were being created without the ‘include on the top navigation bar’ check box ticked. As a result the link was then not automatically added to the top navigation bar but instead later manually added and so was not a security trimmed link.

Display this site on the top link bar of the parent site
Display this site on the top link bar of the parent site

It was then after removing permissions to the various sites that it became clear that users were able to see the top navigation bar link to the sites even though they did not have access.

Obviously there are situations when users don’t have permissions to a site and you don’t want them to see that the site even exists. An example of this might be in an extranet scenario, when you have third parties accessing project sites and you don’t want those third parties seeing the names of other project sites that may exist let alone the content…so how do we prevent this?

Identifying security trimmed links

By reviewing the URLs of the links in top navigation bar I was able to identify whether the links were security trimmed or not. If the field for the URL is disabled then the link is security trimmed and most probably created when as site was created.

Custom top navigation link that is not security trimmed
Custom top navigation link that is not security trimmed
Security trimmed top navigation link
Security trimmed top navigation link

Adding new security trimmed links

After identifying the problem, I then had to make the existing links security trimmed. I did this in two stages. The first was to make a note of the position of the link that needed to be replaced. I then deleted it from the top navigation bar using the ‘Top Link Bar’ site settings page (_layouts/topnav.aspx). The second stage was then to create the new security trimmed link by using the PowerShell code below.

[code lang=”powershell”]
$SPWeb = Get-SPWeb http://demo.jcallaghan.com/
$Nav = $SPWeb.Navigation.TopNavigationBar
$NewLink = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Site Name", "/sitename/default.aspx")
$Nav.AddAsLast($NewLink)
[/code]

Modify the $SPWeb and @(“Site Name”, “/sitename/default.aspx”) arguments as required and run the code for each of the top navigation bar links that need to be security trimmed. Remember the old link will need to be removed and the new one ordered as required.

Conclusion

It appears SharePoint, specifically SharePoint Foundation 2010 only honours security trimmed links in the top navigation when the links are created automatically as opposed to being created manually.

Note: this post specifically targets SharePoint 2010 Foundation which does not include the extended navigation that is included as part of the Publishing feature.

Adding a Yes, No, Cancel prompt to a PowerShell script

When I write PowerShell scripts I tend to want to step through sections, this allows me to decided whether to proceed with sections or not. I achieve this by using the ChoiceDescription class as demonstrated below.

[code lang=”powershell”]
## The following four lines only need to be declared once in your script.
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
$cancel = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel","Description."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no, $cancel)

## Use the following each time your want to prompt the use
$title = "Title" $message = "Question?"
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result) {
0{
Write-Host "Yes"
}1{
Write-Host "No"
}2{
Write-Host "Cancel"
}
}
[/code]

More information about using prompts in PowerShell can be found in the following article and tip on Microsoft TechNet.

Clear the SharePoint Quick Launch using PowerShell

Today I had a requirement to remove all the headings and links from the quick launch navigation of hundreds of SharePoint sites. The sites were being provisioned as part of a PowerShell deployment script that was deleting the default list and libraries. Going through each of these sites manually was not an option – so I edited the deployment script to include a function to remove the headings for me.

SharePoint Quick Launch with Headings
SharePoint Quick Launch with Headings

I remembered doing something similar to this back on SharePoint 2007 but I didn’t have access to the previous script or project and instead had to research the subject for a while to find what I needed.

Solution

A post from Get-SPScripts supplied me with what I was after, although it was part of a much larger script. So I picked away at their code and made it into the following PowerShell function to re-use in other projects.

[code lang=”powershell”]
function Remove-SPQuickLaunchLinks($url){
$SPWeb = Get-SPWeb $url
$QuickLaunch = $SPWeb.Navigation.QuickLaunch
$Nodes = @()

$QuickLaunch | ForEach-Object {
$Nodes = $Nodes + $_.Id
}

$Nodes | ForEach-Object {
$Node = $SPWeb.Navigation.GetNodeById($_)
$Node.Delete()
}
$SPWeb.Dispose()
}

Remove-SPQuickLaunchLinks "http://sharepoint.jcallaghan.com/"
[/code]

The above Remove-SPQuickLaunchLinks function will remove all headings and links from the SharePoint quick launch for a particular site.

Empty SharePoint Quick Launch
Empty SharePoint Quick Launch

Download

You can download the script here. Remember to review, rename and test this script before using it in a production environment.