SharePoint Online: Find All Unused Sites based on Last Modified Date

Requirement: Get All Unused SharePoint Online Sites

PowerShell to Find Unused Sites in SharePoint Online
Let's find the Last Modified Date value of a given site using PowerShell.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables 
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"

Try {
    #Setup Credentials to connect
    $Cred= Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
         
    #Get the Web
    $Web = $Ctx.web
    $Ctx.Load($Web)
    $Ctx.ExecuteQuery()
 
    #Get the last modified date of the web
    Write-host "Last Modified:"$Web.LastItemUserModifiedDate
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
Let's modify this script a bit to retrieve the last modified date value of all subsites of the given site collection.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables 
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"

#Get Credentials to connect
$Cred= Get-Credential

Function Get-SPOSiteLastModified($SiteURL)
{
    Try { 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
         
        #Get the site
        $Web = $Ctx.web
        $Ctx.Load($Web)
        $Ctx.ExecuteQuery()
 
        #Get the last modified date of the site
        Write-host "Last Modified Date of $($SiteURL):"$Web.LastItemUserModifiedDate

        #Get Subsites of the site
        $Webs = $Web.Webs
        $Ctx.Load($Webs)
        $Ctx.ExecuteQuery()
        ForEach($SubWeb in $Webs)
        {
            #Call the function to get subsite's last modified date                
            Get-SPOSiteLastModified $SubWeb.URL
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Call the function to get last modified date of a site and its subsites
Get-SPOSiteLastModified $SiteURL

Get Inactive SharePoint Online Sites using  PowerShell
How about finding inactive SharePoint site collections in the tenant?
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables 
$AdminSiteURL ="https://crescent-admin.sharepoint.com"
$CSVFile = "C:\Temp\SiteData.csv"

#Get Credentials to connect
$Cred= Get-Credential

#Connect to Admin Center
Connect-SPOService -Url $AdminSiteURL -Credential $Cred

#Get All Site Collections
$Sites = Get-SPOSite -Limit All 
$SiteData = @()

#Loop through each site collection
ForEach($Site in $Sites)
{
    Write-host -f Yellow "Getting Data for Site:" $Site.URL
    Try { 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Site.URL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
         
        #Get the Root Web
        $Web = $Ctx.web
        $Ctx.Load($Web)
        $Ctx.ExecuteQuery()
 
        #Get the last modified date of the site
        $SiteData += New-Object PSObject -Property @{
            SiteTitle = $Web.Title
            URL = $Web.Url
            LastModified= $Web.LastItemUserModifiedDate
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
#Export the results to CSV
$SiteData | Export-CSV $CSVFile -NoTypeInformation

Here is the SharePoint Online inactive sites report.
sharepoint online find inactive sites using powershell

PnP PowerShell to Get Unused Site Collections in the past 30 days
$TenantAdminURL = "https://Crescent-Admin.SharePoint.com"
$DaysInActive = 30 # number of days to check since last modified
$CSVPath = "C:\Temp\InactiveSites.csv"

#Connect to Admin Center
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred
    
#Get All Site collections - Exclude BOT, Video Portals and MySites
$SiteCollections = Get-PnPTenantSite -Filter "Url -like $TenantURL -and Url -notlike '-my.sharepoint.com/' -and Url -notlike '/portals/'"

#Calculate the Date
$Date = (Get-Date).AddDays(-$DaysInActive).ToString("MM/dd/yyyy")

#Get All Site collections where the content modified
$InActiveSites = $SiteCollections | Where {$_.LastContentModifiedDate -le $Date} | Select Title, Url, LastContentModifiedDate, Template, StorageMaximumLevel

#Export to CSV
$InActiveSites
$InActiveSites | Export-Csv -Path $CSVPath -NoTypeInformation

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.