12/03/2015

SharePoint: Generate a report of inactive sites and owners

This article is for SharePoint 2007, 2010, 2013 and 2016 on premises. (2007 users see the note at the end of the article.)

Cleaning up inactive sites is a common best practice administrator activity. Finding these sites and getting a list of who to contact can take a bit of work. Here's a little PowerShell script to generate a report of all inactive sites, their Site Collection Administrators and their Full Control users.

 

Inactive Site?

Each web has a property named LastItemModifiedDate that can be used as one criteria for site activity. I say "one criteria" as the site may not have had been updated in months, but users still visit the site and view the content every day. In the scripts below we check for the inactivity using a Where that tests against a calculated date:

Where { $_.LastItemModifiedDate -lt ((Get-Date).AddMonths(-9)) }

You can change the test date by using AddDays, AddMonths and AddYears with negative numbers to "go back in time". The example above looks for webs not updated in the last 9 months.

Note: The LastItemModifiedDate (and all other internal SharePoint dates) is in GMT. To convert to local time use ".ToLocalTime()".

image

 

Create a report…

The following script generates a report that lists each web, last updated date, Site Collection Administrators and Full Control users. Note that if you added an Active Directory group with Full Control, only the group name will be listed, not all of the member users.

image

Get-SPSite -Limit All | 
  Get-SPWeb -Limit All | 
  Where { $_.LastItemModifiedDate -lt ((Get-Date).AddMonths(-9)) } |
  foreach {
    # the URL
    ""   #blank line
    $_.url
    "  LastItemModifiedDate: " + $_.LastItemModifiedDate
    "  Web has unique permissions: " + $_.HasUniquePerm

    # the Site Collection Administrators
    " Site Collection Administrators:"
    foreach ($user in $_.SiteAdministrators)
    {      
       "  " + $user.DisplayName + " " + $user.UserLogin
    }

    # full control users
    " Full Control Users:"
    foreach ($user in $_.Users)
    {
       if ( $_.DoesUserHavePermissions($user,[Microsoft.SharePoint.SPBasePermissions]::FullMask) )      
       { 
         "  " + $user.DisplayName + " " + $user.UserLogin
       }
    }
  }

 

Create a customized SPWeb object for further pipelining…

If you would rather have an output that can be piped on to other cmdlets then use the following script. It adds a PowerShell NoteProperty to each SPWeb object named FullControlUsers can be accessed as if it was a built-in property.

Get-SPSite -Limit All | 
  Get-SPWeb -Limit All | 
  Where { $_.LastItemModifiedDate -lt ((Get-Date).AddMonths(-9)) } |
  foreach {
  
    $TTNusers = @();   #empty collection

    foreach ($user in $_.Users)
    {
       if ( $_.DoesUserHavePermissions($user,[Microsoft.SharePoint.SPBasePermissions]::FullMask) )      
       { 
         $TTNusers += $user;  # add to the collection
       }
    }

    # add the new property
    $_ | Add-Member -type NoteProperty -name FullControlUsers -value $TTNusers;

    # forward the modified SPWeb object on through the piepline.
    $_;

  } | 
  Select Url, SiteAdministrators, FullControlUsers 

image

Or maybe change the "Select" to "Format-List":

image

 

Performance Tip

If this script is run during business hours on a large farm you could impact your users. Consider adding a Start-Sleep cmdlet just after the foreach to insert a small delay between webs. But of course, that will make your script take longer to complete.

image

.

For SharePoint 2007

Replace:

Get-SPSite -Limit All | Get-SPWeb -Limit All |

With:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration")
$webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://maxsp2013wfe")
$webapp.sites | Select -ExpandProperty AllWebs |

 

..

No comments:

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.