SharePoint On-Premises : PowerShell script to find out last content modified of all site collections in a web application – required in SharePoint migration

SharePoint On-Premises : PowerShell script to find out last content modified of all site collections in a web application - required in SharePoint migration
SharePoint On-Premises : PowerShell script to find out last content modified of all site collections in a web application - required in SharePoint migration

Hi All,

Greetings for the day 🙂 LIFE IS BEAUTIFUL 🙂

Today one more PowerShell script required while doing migration

Use case :

  • We are migrating our SharePoint 2013 On-Premises web application to SharePoint Online
  • We are migrating content and plus new development to SharePoint Online
  • In On-Premises environment we have around 200+ my site collections
  • Since now users are started using there OneDrive for business, no need to migrate my site collections
  • But we just need to verify once that is there any latest activity recently happen by users on their respective my site collections
  • Hence the requirement born 🙂

Note : I am sharing here basic steps, you can make it more advanced by adding logs / making parameterized

Our approach :

Of course PowerShell script to scan through all my site collections and find out “LastContentModifiedDate“, “LastSecurityModifiedDate” and export to CSV file

Detailed steps :

  • Add Microsoft SharePoint snap-in to the current session of PowerShell
Add-PsSnapin Microsoft.SharePoint.PowerShell

  • The Start-SPAssignment cmdlet properly disposes of objects used with variable assignments.
Start-SPAssignment -Global

  • CSV file path where we need to export
#CSV file path
$path = "D:/Knowledge Junction/PowerShell Scripts/mysiteslastmodifieddetails.csv"

  • Fetch the web application – here in our case – My Site web application
#Fetch web application
$webApp = Get-SPWebApplication -Identity "https://mysite.knowledgejunction.com/"

  • Create empty array variable to store the result
#create empty array variable - to store the result
$Report = @()

  • Loop through all the site collections of above web applications and fetch the respective properties and store in object
foreach($spSite in $webApp.Sites)
{
            #$spSite.LastContentModifiedDate
            #$spSite.LastSecurityModifiedDate
                      
            $objSiteData = New-Object Pscustomobject -Property @{
                SiteURL = $spSite.URL
                LastModifiedDate = $spSite.LastContentModifiedDate
                LastSecurityModifiedDate = $spSite.LastSecurityModifiedDate
                Owner = $spSite.Owner
            }#object
            $Report += $objSiteData

            #displaying site URL in console
            Write-Host "Site - " $spSite.Url
            Write-Host " "
}#foreach

  • Finally export result to CSV file
$Report | Export-Csv $Path -NoTypeInformation

Complete PowerShell script :

#PowerShell script to get last modified properties of site collection from give web application

#Add Microsoft SharePoint snap-in to the current session of PowerShell  
Add-PsSnapin Microsoft.SharePoint.PowerShell

#The Start-SPAssignment cmdlet properly disposes of objects used with variable assignments.
Start-SPAssignment -Global

#CSV file path
$path = "D:/Knowledge Junction/PowerShell Scripts/mysiteslastmodifieddetails.csv"

#Fetch web application
$webApp = Get-SPWebApplication -Identity "https://mysite.knowledgejunction.com/

#create empty array variable - to store the result
$Report = @()

foreach($spSite in $webApp.Sites)
{
            #$spSite.LastContentModifiedDate
            #$spSite.LastSecurityModifiedDate
                      
            $objSiteData = New-Object Pscustomobject -Property @{
                SiteURL = $spSite.URL
                LastModifiedDate = $spSite.LastContentModifiedDate
                LastSecurityModifiedDate = $spSite.LastSecurityModifiedDate
                Owner = $spSite.Owner
            }#object
            $Report += $objSiteData

            #maintain the the logs
            Write-Host "Site - " $spSite.Url
            Write-Host " "
}#foreach


#export the result to CSV file on given path
$Report | Export-Csv $Path -NoTypeInformation

On Knowledge-Junction you will find lots of PowerShell script required for SharePoint Migration, SharePoint On-Premises and Microsoft 365. Please either search or find at – https://knowledge-junction.in/category/technology-articles/powershell-cmdlets/

Thanks for reading 🙂 HAVE A GREAT TIME AHAEAD 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.