PowerShell Script To Get Web Application And Site Collection Details

In this article, I would like to share the steps to get a site collection details in the farm. This solution can be applied for SharePoint 2010, 2013 and SharePoint 2016.

If we are going to Google it, we will see many results which will giveus exact details in one script. In case we have the multi-farm or single SharePoint server farm, and the requirement is to get site collection details with the below information, we need to collect the web application's and site collection's details from the farm, like Web Application name, URL, Site collection Name, URL, Content DB, Size of Content DB, Created Date, Site owner and others in CSV format.

Below is the script which will perform this activity for us:

  1. If ((Get-PSSnapin | where {$_.Name -match "SharePoint.PowerShell"}) -eq $null)  
  2. {  
  3.     Add-PSSnapin Microsoft.SharePoint.PowerShell  
  4. }  
  5. Clear-Host  
  6.   
  7.   
  8. $ServerName = HostName #Bring your Server Name  
  9. $webapps = Get-SPWebApplication  
  10. $Output = "C:\$ServerName.csv" #Output file  
  11.    
  12.   
  13. $SiteDataCollection = @()  # Array to store data  
  14. foreach ($webapp in $webapps){  
  15.   
  16. $webappurl = $webapp.URL;  
  17. $webappname=$webapp.DisplayName  
  18. $sites=get-spsite -limit all -WebApplication $webapp.URL  
  19.   
  20. foreach ($site in $sites)  
  21. {  
  22. $ContentDB = Get-SPContentDatabase -site $site.url  
  23.   
  24.             $SiteData = New-Object PSObject   
  25.             $SiteData | Add-Member -type NoteProperty -name "Web Application URL" -value $webappurl  
  26.             $SiteData | Add-Member -type NoteProperty -name "Web Application Name" -value $webappname   
  27.             $SiteData | Add-Member -type NoteProperty -name "Site URL" -value $site.url     
  28.             $SiteData | Add-Member -type NoteProperty -name "Database Name" -value $ContentDB.Name   
  29.             $SiteData | Add-Member -type NoteProperty -name "Database Size" -value $($ContentDB.DiskSizeRequired /1024/1024)  
  30.             $SiteData | Add-Member -type NoteProperty -name "Date Created" -value $site.RootWeb.Created    
  31.             $SiteData | Add-Member -type NoteProperty -name "OWner" -value $site.Owner        
  32.               
  33.             $SiteDataCollection += $SiteData  
  34.   
  35. }  
  36.   
  37. }  
  38.   
  39. $SiteDataCollection | Export-Csv -Path $Output -Force  
  40.   
  41. Write-Host "Successfully Completed" -ForegroundColor DarkRed   

Copy the above script in PowerShell ISE of any SharePoint Server in the farm and click on Start.

You will be getting the result on the defined path with the server name.