Archive for the ‘SPUsageReportType SharePoint Powershell Usage Statistics’ Tag

Usage Data in SharePoint – PowerShell retrieval – Last Accessed Date for a Site Collection   1 comment

This is  a three line(ish) method of giving Powershell love to the SharePoint 2007 Usage Stats you need to get “Last Accessed Date” from your site collection…It works for me, if its wrong or you have comments, please do leave me a note 🙂

For me the use of this is to give me a “Last Accessed Date” for a site collection, as I couldn’t see (I didn’t look far) another way of quickly getting that information from the SharePoint API.

$web.Usage doesn’t cut it for us as we have stuff that modifies the site running in the background, and it doesn’t provide a last access date anyways.

So, let on with the good stuff…I’m assuming you have loaded the SharePoint main and Administration assemblies…and have a Site Collection attached to $web

$ReportType = [Microsoft.SharePoint.Administration.SPUsageReportType]::url
$ReportPeriod = [Microsoft.SharePoint.Administration.SPUsagePeriodType]::lastMonth
$usage = $web.GetUsageData($ReportType, $ReportPeriod)

Wibble through all the site URLS with a loop of your choosing…then do a sort on the date to get the last one.  Peasey!

If you do a lookup of the ReportType methods you can get lots more information out from the usage statistics.

Not checked against 2010, but works fine in 2007, so all things being reasonably equal, should work in 2010 also. 

Resulting object coming back is a DataRow.

I ferttle through that with a:

foreach ($row in $usage.PSBase.Rows) {<your stuff here>}  as for me thats the easiest way of getting the Most Recent Day column data on a URL.

thus: $thisRecentDay =  $row.”Most Recent Day”

Personally I stuff that into an array, loop to collect ’em all and do a sort.

Then I figured I was in a world of hurting, as I had sites with UK, and sites with US date formats, deepness of joy!


try {

$Global:ordered = $Global:mostRecentDayArray | Sort-Object {[DateTime]::Parse($_.Date)}

} # Should sort your local culture stuff.


$Global:ordered = $Global:mostRecentDayArray | Sort-Object {[DateTime]::ParseExact($_.Date,”Mdyyyy”, [System.Globalization.CultureInfo]::InvariantCulture)}
 }  # Should take care of other stuff…change your M d and yyyy’s as required.

That’ll take care of the date parse.

Once sorted, just grab the last date, and you have the last accessed date for your site collection…

$Global:lastDataAccessed = $Global:ordered[$Global:ordered.count-1] (you’ll need add another line and a logic check to see how many entries are in the array…

Handy for finding last access information for your sites stats page!