Introduction
This article explains how to obtain the last modified date of all the sites in a Farm using PowerShell.
Prerequisites
- Ensure you have a SharePopint Server.
- 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.
- Open a new text file and copy the following code and paste it into the file.
- $Today = [string]::Format( "{0:dd-MM-yyyy}", [datetime]::Now.Date )
- $TranscriptFileName = "SitesSubsites_$Today.txt"
- Start-Transcript -path $TranscriptFileName -append
- [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
- $OFS = "`r`n"
- $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
- $websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
- $webapps = @()
- $i=0
- $totalNoSites=0
- $a =(Get-Date).AddMonths(-6)
- write-host "6 months before date-->"
- write-host $a.ToShortDateString()
- foreach ($websvc in $websvcs) {
- //loop through each webapplication
- foreach ($webapp in $websvc.WebApplications) {
- //loop through each site collection
- foreach ($site in $webapp.Sites) {
- //loop through each site in a web
- foreach ($web in $site.AllWebs) {
- $totalNoSites++
- if ($web.LastItemModifiedDate -le $a)
- {
- Write-Host "Old site"
- $i++
- write-host "Web URL -->" $web.URL
- write-host " Last Modified Date -->"$web.LastItemModifiedDate.ToShortDateString()
- write-host ""$OFS
- }
- else
- {
- Write-Host "Recently accessed sites"
- write-host "Web URL -->" $web.URL
- write-host " Last Modified Date -->"$web.LastItemModifiedDate.ToShortDateString()
- write-host ""$OFS
- }
- }
- }
- }
- }
- write-host ""$OFS
- Write-host "Total no. of sites : " $totalNoSites
- write-host ""$OFS
- Write-host "Total no. of sites not accessed for 6 months : " $i
- write-host ""$OFS
- write-host "Request Completed"
- Stop-Transcript
- Save the file in a .ps1 file format.
- The “LastItemModifiedDate” property gets you the information of the last modified date of a site.
Testing
- Right-click on the file and click on “Run with PowerShell”.

- 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.
Chandni MehtaPosted Oct 21, 2020, 5:51 AM
Can we get recently modified sub sites in SharePoint online using Powershell?
Madhu MPosted Nov 5, 2015, 5:25 PM
I would like to add some conditions in else IF because if any website is broken it is not considering as Not USED. If there is any errors similar to below one is not considered as notused. //loop <<<< through each site in a web + CategoryInfo : ObjectNotFound: (//loop:String) [], CommandNotFo undException + FullyQualifiedErrorId : CommandNotFoundException Recently accessed sites Web URL --> http://nature/personal/1002strp Last Modified Date --> 11/5/2015
Madhu MPosted Nov 5, 2015, 5:22 PM
Do you have C# version for the same scenario?