Guy, who has an overview about his stuff

SharePoint Site Usage: Get active Sites for last 6 months with PowerShell

Hi folks, when migrating our SharePoint to SharePoint Online, it can be really hard to separate the wheat from the chaff. SharePoint Site usage helps you find, what is actually used in your current farm. SharePoint farms grow by new projects and with the change of structures. If you want to have an overview, it is every time a good idea, to get rid of old stuff.

Administrators, who is searching for SharePoint Site usage

In a previous article, I showed you how to get all sites and subsites in SharePoint Online. This is a good basis, but if you need more, you should definitely continue reading. In this article, I want to show you one of my tools, which shows you the SharePoint site usage for previous months, so you understand what is needed in your new environment and what’s not. I am getting the data by using the interface IAnalyticsItemData of the search analytics component.

Prerequisites

  • SharePoint Search is up and running
  • You have access to SharePoint Server with a highly privileged account (SP_Admin or SP_Farm)

Description of the PowerShell script

This script iterates through all sites and asks the Search service application for the roll-up analytics data for the month specific month. The offset is the integer, which states a gap between the current month and the offset month.

Example:

If the variable PrevMonthOffset is 3, it is 3 months ago from the current month.

After you run the script and export will be created at your user’s desktop with all the site usage for the previous months. The current configuration in the script shows the SharePoint Site usage for the previous six months ($PrevMonthOffset = 6). You can change it for more months, if you need to.

SharePoint Site Usage PowerShell script

If you run the script like this, you do get the SharePoint site usage statistics for the last six months.

param (
    $DesktopPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop),
    $global:exportpath = $DesktopPath + "\sitestats.csv",
    $PrevMonthOffset = 6
)

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

$global:SuccesCount = 0
$global:ErrorCount= 0

$SearchApp = Get-SPEnterpriseSearchServiceApplication

function Get-Stats {
    param
    (
        $SiteCollection,
        $Subsite,
        $PrevMonthOffset
    )

    $Offset = $PrevMonthOffset
    while ($Offset -lt 6)
    {
        [System.Int32]$Hits =0
        [System.Int32]$UniqueVisitor =0
        $Offsetfactor = -1 * $Offset
        $RootResult = $SearchApp.GetRollupAnalyticsItemData(1,[System.Guid]::Empty,$SiteCollection.ID,$Subsite.id)
        $Date = (Get-Date).AddMonths($Offsetfactor)    
        
        $RootResult.GetMonthlyData($Date,[REF]$Hits,[REF]$UniqueVisitor)
        $HitsPreviousName = "_HitsPrevious"+$Offset + "month" 
        $UniqueVisitorName = "_UniqueVisitor" +$Offset +"month"

        $Subsite | Add-Member -MemberType NoteProperty -Name $HitsPreviousName -Value $Hits -Force
        $Subsite | Add-Member -MemberType NoteProperty -Name $UniqueVisitorName -Value $UniqueVisitor -Force


	    
            
        $Offset ++
    }
    try
    {
        $Subsite | Select-Object url, *_* | Export-Csv -Path $exportpath -Append -Delimiter ";" -NoTypeInformation -ErrorAction Stop
        $global:SuccesCount++
    }
    catch
    {
        $Subsite.url
        $global:ErrorCount++
    }
}

$SiteCollections = Get-SPSite -Limit All

Foreach ($SiteCollection in $SiteCollections)
{
    $SiteCollection.AllWebs | ForEach-Object{ Get-Stats -SiteCollection $SiteCollection -Subsite $_ -PrevMonthOffset $PrevMonthOffset}

}

Write-Host "success: $global:SuccesCount error: $global:ErrorCount "

Further reading

There are a few more methods the analytics item data provides. You can find them here:

GetDailyData(DateTime, Int32, Int32)
GetHitCountForDay(DateTime)
GetHitCountForMonth(DateTime)
GetMonthlyData(DateTime, Int32, Int32)
GetUniqueUsersCountForDay(DateTime)
GetUniqueUsersCountForMonth(DateTime)

IAnalyticsItemData Interface (Microsoft.Office.Server.Search.Analytics) | Microsoft Docs

Image reference

Data vector created by stories – www.freepik.com

Image by mohamed Hassan from Pixabay

This Post Has One Comment

Leave a Reply