Find Top 10 Site Collections based on its Usage during Last Month

PowerShell Script to find top 10 SharePoint 2007 site collections based on its usage hits:

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Functions to Imitate SharePoint 2010 Cmdlets in MOSS 2007
function global:Get-SPWebApplication($WebAppURL)
{ 
  return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
}
 
function global:Get-SPSite($url) {
    return new-Object Microsoft.SharePoint.SPSite($url)
 } 
 
function global:Get-SPWeb($url) {
  $site= New-Object Microsoft.SharePoint.SPSite($url)
        if($site -ne $null) {
               $web=$site.OpenWeb();        
            }
    return $web
} 

#Get all site collections of the provided web application
$WebApp = Get-SPWebApplication "https://intranet.crescent.com"
$SitesCollection = $WebApp.Sites

$UsageDataCollection = @()
 #Iterate through each site collection
 ForEach($site in $SitesCollection)
 {
    $web = Get-SPWeb $site.URL
    $LastMonthUsage = ($web.GetUsageData("url", "lastMonth") | sort 'Total Hits')
    If($LastMonthUsage -ne $null)
    { 
  
        $totalHits = ($LastMonthUsage | Measure-Object 'Total Hits' -Sum | Select -expand Sum)     
        
        $UsageDataResult = New-Object PSObject
        $UsageDataResult | Add-Member -type NoteProperty -name "Site Collection URL" -value $site.URL
        $UsageDataResult | Add-Member -type NoteProperty -name "Site Title" -value $web.title
        $UsageDataResult | Add-Member -type NoteProperty -name "Total Hits" -value $totalHits

        $UsageDataCollection += $UsageDataResult

        #Clear variables
        $LastMonthUsage = $null
        $tHits = $null
    }
 }
 
 $UsageDataCollection | sort-object "Total Hits" -descending | select-object -first 10 | Export-csv "Top10Sites.csv" -notypeinformation 

For SharePoint 2010 and above, Use Web Analytics Web Part!

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

One thought on “Find Top 10 Site Collections based on its Usage during Last Month

  • i tried this script on sharepoint 2013…but it is not working

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *