Checking Your SharePoint 2010 Sites For Stale Content

Scott ZischerkOne of the best ways of keeping your users using your SharePoint site is keep the content fresh.  You could do this manually, but depending on how large you site collection is this could take hours or even days.  Or, using PowerShell it could take seconds.

SharePoint updates the last item modified date and time for a site every time a list item or document library is modified, we can use this property on the site to determine whether a site needs to be updated, or possibly removed if no one has kept it up  to date.

To start the script, first we’ll need a to load up the SharePoint PowerShell context.

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

I like adding this in an if block to prevent an error from being loaded twice.  Next, we’ll need a bit of recursion to go drill down through the SharePoint site tree structure.

function GetWebLastModifiedDate($currentWeb)
{
    Write-Output $currentWeb | Select Url, Title, LastItemModifiedDate
    
    foreach($subWeb in $currentWeb.webs)
    {
        Write-Output $subWeb | Select Url, Title, LastItemModifiedDate
        if($subWeb.webs.count > 0)
        {
            GetWebLastModifiedDate $subWeb
        }    
    }
}

We’ll write out the Url, Title and LastItermModifiedDate to the screen before we drill down to the sub sites of the current site.  Finally, we’ll need just one more object, a site collection.  For the script, we’ll just pass the url string for the site collection in and then query for the actual site collection object.

$siteUrl=$args[0]
$site = get-spsite $siteUrl

You’ll notice the args[0], this is the built in way of passing parameters into a PowerShell script, there are other ways, but this will do what we want just fine.  Now we just call the recursive function at the end of the script

GetWebLastModifiedDate $site.rootweb

Now, to run the script, you’ll need to run it on your SharePoint 2010 server. Open up a command prompt and type

      powershell.exe {path to script}\{script name} ”{site collection url}”

Example:

      powershell.exe c:\scripts\GetSiteLastModified.ps1 “http://localhost”

Putting this all together and running the script will give you the following output:

Url                                 Title                  LastItemModifiedDate
—                                    —–               ——————–
http://localhost                       Home                4/27/2012 1:27:19 PM
http://localhost/clients               Clients             4/27/2012 1:10:07 PM
http://localhost/Departments           Departments         4/24/2012 1:30:23 PM
http://localhost/HR                    Human Resources     4/27/2012 2:16:45 PM
http://localhost/managerresources      Manager Resources   4/27/2012 1:02:31 PM
http://localhost/Marketing             Marketing           4/27/2012 1:03:39 PM
http://localhost/Search                Search              4/24/2012 12:00:06 PM
http://localhost/training              Training            4/27/2012 1:07:56 PM

From here you can copy and paste or, since it’s PowerShell, you can write directly to a file.

I have included the complete script here.  Just unzip it to a folder of your choice and run it.  Enjoy!

2 thoughts on “Checking Your SharePoint 2010 Sites For Stale Content

  1. Hi!

    Great script. Can this be scoped to display all the site collections in a web application, for say “no modified content greater than 30 days”?

    Thanks!

    MJM

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s