Obtaining Last Modified Date of All the Sites in SharePoint

Introduction

This article explains how to obtain the last modified date of all the sites in a Farm using PowerShell.

Prerequisites

  1. Ensure you have a SharePopint Server.
  2. Ensure you have a PowerShell window.

User requirements

  • Get all the site details in the Farm that are not accessed for more than 6 months.

  • Get all the site details in the Farm that are not accessed for more than 1 year.

Use the following procedure.

  1. Open a new text file and copy the following code and paste it into the file.
    1. $Today = [string]::Format( "{0:dd-MM-yyyy}", [datetime]::Now.Date )  
    2. $TranscriptFileName = "SitesSubsites_$Today.txt"  
    3. Start-Transcript -path $TranscriptFileName -append  
    4.   
    5. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null  
    6. $OFS = "`r`n"  
    7. $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local   
    8. $websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}   
    9. $webapps = @()  
    10. $i=0  
    11. $totalNoSites=0  
    12. $a =(Get-Date).AddMonths(-6)  
    13.   
    14. write-host "6 months before date-->"  
    15. write-host $a.ToShortDateString()  
    16.   
    17.   
    18. foreach ($websvc in $websvcs) {   
    19.         //loop through each webapplication  
    20.   foreach ($webapp in $websvc.WebApplications) {   
    21.   //loop through each site collection  
    22.     foreach ($site in $webapp.Sites) {   
    23.        //loop through each site in a web  
    24.        foreach ($web in $site.AllWebs) {   
    25.       
    26.           $totalNoSites++  
    27.   
    28.       
    29.           if ($web.LastItemModifiedDate -le $a)   
    30.           {  
    31.              Write-Host "Old site"  
    32.              $i++  
    33.              write-host "Web URL -->" $web.URL  
    34.              write-host " Last Modified Date -->"$web.LastItemModifiedDate.ToShortDateString()              
    35.              write-host ""$OFS  
    36.           }  
    37.           else  
    38.           {  
    39.              Write-Host "Recently accessed sites"  
    40.              write-host "Web URL -->" $web.URL  
    41.              write-host " Last Modified Date -->"$web.LastItemModifiedDate.ToShortDateString()              
    42.              write-host ""$OFS  
    43.   
    44.           }   
    45.        }  
    46.   
    47.      }  
    48.   
    49.    }   
    50. }  
    51. write-host ""$OFS  
    52. Write-host "Total no. of sites : " $totalNoSites  
    53. write-host ""$OFS  
    54. Write-host "Total no. of sites not accessed for 6 months : " $i  
    55. write-host ""$OFS  
    56. write-host "Request Completed"  
    57.   
    58. Stop-Transcript
  2. Save the file in a .ps1 file format.

  3. The “LastItemModifiedDate” property gets you the information of the last modified date of a site.

Testing

  1. Right-click on the file and click on “Run with PowerShell”.

    Run with PowerShell

  2. Data will be extracted from the Farm and the site last modified date will be displayed in the PowerShell window and finally the data will be stored in the text file.

Summary

Thus in this article you saw how to obtain the last modified date of all the sites in a Farm using a PowerShell Script.