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

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. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
    5. $OFS = "`r`n"
    6. $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
    7. $websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
    8. $webapps = @()
    9. $i=0
    10. $totalNoSites=0
    11. $a =(Get-Date).AddMonths(-6)
    12. write-host "6 months before date-->"
    13. write-host $a.ToShortDateString()
    14. foreach ($websvc in $websvcs) {
    15. //loop through each webapplication
    16. foreach ($webapp in $websvc.WebApplications) {
    17. //loop through each site collection
    18. foreach ($site in $webapp.Sites) {
    19. //loop through each site in a web
    20. foreach ($web in $site.AllWebs) {
    21. $totalNoSites++
    22. if ($web.LastItemModifiedDate -le $a)
    23. {
    24. Write-Host "Old site"
    25. $i++
    26. write-host "Web URL -->" $web.URL
    27. write-host " Last Modified Date -->"$web.LastItemModifiedDate.ToShortDateString()
    28. write-host ""$OFS
    29. }
    30. else
    31. {
    32. Write-Host "Recently accessed sites"
    33. write-host "Web URL -->" $web.URL
    34. write-host " Last Modified Date -->"$web.LastItemModifiedDate.ToShortDateString()
    35. write-host ""$OFS
    36. }
    37. }
    38. }
    39. }
    40. }
    41. write-host ""$OFS
    42. Write-host "Total no. of sites : " $totalNoSites
    43. write-host ""$OFS
    44. Write-host "Total no. of sites not accessed for 6 months : " $i
    45. write-host ""$OFS
    46. write-host "Request Completed"
    47. 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.