Home > PowerShell, SharePoint 2010, SharePoint Advancements > Get SharePoint 2010 Site Collections Last Modified Date using PowerShell

Get SharePoint 2010 Site Collections Last Modified Date using PowerShell

I was recently asked if I could provide a script that will list all site collections withing a web application that had not been accessed within a given amount of days so that the Site Administrators can decommission them due to non use.   Since there is no easy way to determine who has actually “visited” these sites without digging through audit logs or IIS logs, or possibly writing some custom code, the next best thing to do is to check when the site was last modified.  The assumption here is if a site was created on a specific date, but nothing has changed on the site (added documents, document/library creations, etc) since a given amount of time the site is probably “stale” and no longer in use.

The below PS script basically loops through all Site Collections / Sub-Sites of a Web Application and determines when it was last modified based off a given date, and spits out a report.

——————————————————————————————————————————————

$webApp = Get-SPWebApplication “URL OF WEB APPLICATION”
$daysInActive = Read-Host “Enter in number of days to check since last modified”
$date = (Get-Date).AddDays(-$daysInActive).ToString(“MM/dd/yyyy”)

Foreach ($web in $webApp | Get-SPSite -Limit All | Get-SPWeb -Limit All)
{

if ($web.LastItemModifiedDate -le $date)
{

Write-Host $web.Url
Write-Host $web.LastItemModifiedDate

$web.Url + ” | ” + “Last Modified Date: ” + $web.LastItemModifiedDate >> LastModified.txt

}

}

——————————————————————————————————————————————

UPDATE:

The following updated script generates a cvs file, and emails specific individuals who need to be notified of “InActive” Site Collections/Sites

——————————————————————————————————————————————

#Get Web Application
$webApp = Get-SPWebApplication “URL OF WEB APPLICATION”

#Get Today’s Date
$today = Get-Date

#Set EmailSentDate
$EmailSentDate = $today.ToString(“MM-dd-yyyy”)

#Create csv Log Name and Location
$csvLog = “c:\temp\lastModified_” + $EmailSentDate + “.csv”

#Set Count to 0
$count = 0

#CREATE .CVS Column Headers
“siteTitle `t” + “siteURL `t” + “siteOwners `t” + “lastModifiedDate” >> $csvLog

#GET NUMBER DAYS OF INACTIVE FROM USER
$daysInActive = Read-Host “Enter in number of days to check since last modified”
$date = (Get-Date).AddDays(-$daysInActive).ToString(“MM/dd/yyyy”)

#LOOP THROUGH ALL SITE COLLECTIONS AND SUB-SITES IN A WEB APPLICATION
Foreach ($web in $webApp | Get-SPSite -Limit All | Get-SPWeb -Limit All)
{
if ($web.LastItemModifiedDate -le $date)
{

#SET $siteOwner VARIABLE TO EMPTY
$siteOwner = “”
#SET COUNT TO
$count = 0

#LOOP THROUGH SITE OWNERS
foreach ($siteAdmin in $web.SiteAdministrators)
{

$count = $count + 1

#IF MORE THAN 1 SITE OWNER, PUT OWNERS ON ONE LINE SEPERATED WITH A COMMA
if ($count -gt 1)
{
$siteOwner = $siteAdmin.LoginName + “, ” + $siteOwner
}
#IF ONLY ONE SITE OWNER
else
{
$siteOwner = $siteAdmin.LoginName
}
}
#WRITE TO .CSV FILE
$web.Title + “`t” + $web.URL + “`t” + $siteOwner + “`t” + $web.LastItemModifiedDate >> $csvLog
}
}

#SEND EMAIL NOTIFICATION
$att1 = New-Object Net.Mail.Attachment($csvLog)
$email = New-Object System.Net.Mail.MailMessage
$email.From = “FROM EMAIL ADDRESS”
$email.To.Add(“TO EMAIL ADDRESS”)
$email.Subject = “SharePoint Sites With Inactivity – ” + $daysInActive
$email.Body = “The Following SharePoint sites have not been visited in the past, $daysInActive days.  Please notify the Site Owners of these sites to decommission”
$email.Attachments.Add($att1)
$smtpServer = “SMTP SERVER NAME or IP ADDRESS”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($email)

—————————————————————————————————————————————————————————

Advertisements
  1. Jitendar Singh
    April 10, 2014 at 2:31 pm

    Thanks James! this PS too helpful as per my requirement.

    Just to make it as an excel report with columns Site Title, Site URL, Site owners and site last modified date

    and run at every month end and send an email to set of people – can we add that part too please.

    Thanks,
    Jitendar

    • April 10, 2014 at 4:46 pm

      Hi Jitendar,

      I’ve updated the script to help you out.

      For you to send emails every month at the end of the month, just set up a Task Scheduler on your SharePoint Server to automatically execute the PS script on whatever day you prefer.

      Hopefully this helps.

      v/r
      JShidell

  2. Jitendar Singh
    April 14, 2014 at 12:52 pm

    It’s really great – thanks much!!!

    Can you see this error which I am getting at my email sending section..

    Enter in number of days to check since last modified: 60
    Exception calling “Send” with “1” argument(s): “Failure sending mail.”
    At C:\Users\jsingh\PS\old_sites_v2.ps1:66 char:11
    + $smtp.Send <<<

    Does it anything to do with smtp services – I verified and I don’t see smtp service in the services console.

    Thanks,
    Jitendar

    • April 14, 2014 at 1:03 pm

      Hi Jitendar,

      Yes you will need SMTP services installed on the server for this to work.

      Thanks

      v/r
      JShidell

  3. Jitendar Singh
    April 14, 2014 at 2:11 pm

    Thanks James!

  4. eka.tazio@free.fr
    October 29, 2014 at 1:47 pm

    There is a mistake in this line :
    #WRITE TO .CSV FILE
    $web.Title + “`t” + $web.URL + “`t” + $siteOwner + “`t” + $web.LastItemModifiedDate >> $csvLog
    The point between Web and lastitemmodifieddate, is missing 😉

    • October 29, 2014 at 1:51 pm

      Thanks for the catch. I fat finger all the time!!

  5. tetiana
    November 10, 2014 at 2:08 pm

    Thank you for this post
    Can you help me? I need collect data from sharepoint 2007 and 2010/ Data must be:
    List off all application, site collection, subsite must consist of:
    Site name, Site URL, people with full control permission in site, last change data

    I need this information to understand which sites need to migrate and which inactive and can be remove. Thank you in advance

  1. No trackbacks yet.

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

%d bloggers like this: