Get last modified date of all subsites in a SharePoint tenant

SR VSP 1,226 Reputation points
2023-10-18T04:35:43.8133333+00:00

Hello guys, I'm trying to fetch all subsites last modified date in a Tenant but it is not working when tenant URL is given any advise here ?

# Get all subsites last modified date in a 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
$SiteURL ="https://Contoso-admin.sharepoint.com"
$CSVFilePath = "C:\temp\SubsiteLastModified.csv"

#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

        #Output data to CSV file
        Add-Content -Path $CSVFilePath -Value "$($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
    }
}

#Clear contents of existing CSV file
Clear-Content -Path $CSVFilePath

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

Write-host "Data has been exported to '$CSVFilePath'"

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,827 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,706 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Haoyan Xue_MSFT 20,741 Reputation points Microsoft Vendor
    2023-10-18T08:35:40.09+00:00

    Hi @SR VSP ,

    We can't directly get last modified date of all subsites in a SharePoint tenant.

    We can retrieve the last modified date value of all subsites of the given site collection.

     
    #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
    
    
    

    Reference: https://www.sharepointdiary.com/2019/08/sharepoint-online-find-unused-sites-using-powershell.html#ixzz8GThKMKI1

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.