How to Get a List of Webs, which were not accessed for quite some time – SharePoint PowerShell

Sathish Nadarajan
 
Solution Architect
May 16, 2016
 
Rate this article
 
Views
4715

I was asked to find out the Webs, which were not accessed/modified in recent times. i.e., to find the unused Webs for a quite some time. Hence, I come up with the below script to find them.

The Script itself has all the required comments. I don’t have much to explain about the script.

 ######################### this method will return the SubSites which are older than the given Date
 ############## to be precise, the sites which were not accessed/modified till the given date.
 ######################### This method will export the output to XML
 
 function GetAgedSites()
 {
     $script:Progress = "2:Entered"
     Write-Host "Entered GetAgedSites Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Entered GetSolution Method"
     
     # Get the WebApplication From the Configuration File
     $WebApplication = Get-SPWebApplication $Config.Configuration.WebApplication.URL
 
     # Assign the XML Output File Paths
     $AgedSites_XML_Path = $scriptBase + "Reports-$LogTimeAgedSites.xml"
     
     # Create the XML File Tags
     
     $xmlWriter = New-Object System.XMl.XmlTextWriter($AgedSites_XML_Path,$Null)
     $xmlWriter.Formatting = 'Indented'
     $xmlWriter.Indentation = 1
     $XmlWriter.IndentChar = "`t"
     $xmlWriter.WriteStartDocument()
     $xmlWriter.WriteComment('Aged Sites List On the WebApplication ' + $WebApplication.DisplayName)
     $xmlWriter.WriteStartElement('WebApplication')
     $xmlWriter.WriteEndElement()
     $xmlWriter.WriteEndDocument()
     $xmlWriter.Flush()
     $xmlWriter.Close()
     
     # Get All the SiteCollections
     $SiteCollections =  $WebApplication | Get-SPSite -Limit All
     
     Add-Content "$ProgressFile" "Gathered All the site Collections"
     Add-Content "$ProgressFile" "Iterating through the Site Collections"
     
     # Create the Initial Solution Node
     $xmlDoc = [System.Xml.XmlDocument](Get-Content $AgedSites_XML_Path);
     $siteCollectionNode = $xmlDoc.CreateElement("SiteCollections")
     $xmlDoc.SelectSingleNode("//WebApplication").AppendChild($siteCollectionNode)
     $xmlDoc.Save($AgedSites_XML_Path)
     
     # Iterate through the Site Collections    
     foreach($SiteCollection in $SiteCollections)
     {
         $siteCollectionName = $SiteCollection | select @{label = "Title";Ex = {$_.rootweb.Title}} 
         
          
         # Create the Initial Solution Node
         $xmlDoc = [System.Xml.XmlDocument](Get-Content $AgedSites_XML_Path);
         $siteCollectionNode = $xmlDoc.CreateElement("SiteCollection")
         $xmlDoc.SelectSingleNode("//WebApplication/SiteCollections").AppendChild($siteCollectionNode)
         $siteCollectionNode.SetAttribute("Name", $siteCollectionName.Title)
         $siteCollectionNode.SetAttribute("Url", $SiteCollection.Url)
          
         Add-Content "$ProgressFile" "Get All the Webs"    
         
         #Get all the webs including the Sub-Sub Web
         $Webs = Get-SPWeb -site $SiteCollection -Limit All 
         
         # Iterate through the Webs
         foreach($Web in $Webs)
         {
             $tempContent = "Iterate through the Web : " + $Web.Title
             Add-Content "$ProgressFile" $tempContent
             
             # Check for the LastItemModified Date
             if($Web.LastItemModifiedDate -le $Config.Configuration.FetchSitesBeforeTheDate)
             {
                 # write the output on XML File
                  
                 $webAppNameNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("Web"));
                 $webAppNameNode.SetAttribute("Name", $Web.Title)
                                 
                 $webAppUrlNode = $webAppNameNode.AppendChild($xmlDoc.CreateElement("Url"));
                 $webAppUrlTextNode = $webAppUrlNode.AppendChild($xmlDoc.CreateTextNode($Web.Url));
                 
                 $webAppLastModifiedNode = $webAppNameNode.AppendChild($xmlDoc.CreateElement("LastModified"));
                 $webAppLastModifiedTextNode = $webAppLastModifiedNode.AppendChild($xmlDoc.CreateTextNode($Web.LastItemModifiedDate));
                  
             }
         }
         # write the output on XML File
         $xmlDoc.Save($AgedSites_XML_Path)
     }  
     
     Write-Host "Completed GetAgedSites Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Completed GetAgedSites Method"
     $script:Progress = "2:Success"
 }
 ########### End of Method #################
 
 

Happy Coding,

Sathish Nadarajan.

Category : PowerShell, SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment