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) {
-
- foreach ($webapp in $websvc.WebApplications) {
-
- foreach ($site in $webapp.Sites) {
-
- 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.