SharePoint Pals
 | Sign In
How to Get a List of Webs, which were not accessed for quite some time – SharePoint PowerShell
by Sathish Nadarajan 16 May 2016
Author
SharePoint MVP
Visits   
Today  :   3     Total  :    2676

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-$LogTime\AgedSites.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.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation