Archive

Archive for April, 2014

Get SharePoint 2010 Site Collections Last Modified Date using PowerShell

April 9, 2014 9 comments

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)

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