Get All SharePoint Site Collections in a Web Application using PowerShell

How to get all site collections of a web application in SharePoint?

All site collections and sites within a web application share the same top-level URL. To get all site collections in a web application from SharePoint central Admin, follow these steps:

  1. Go to SharePoint Central Admin Site >> Application Management. 
  2. Click on the “View all site collections” link under Site Collections Group. Select your web application. This page lists all site collections in the selected web application.
    sharepoint get all site collections in web application

Get all site collections in web application using PowerShell:

Here is the PowerShell to list all site collections in a web application.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set the Web application URL
$WebAppURL="https://intranet.crescent.com"

#Get the Web Application        
$WebApp = Get-SPWebApplication $WebAppURL

#Get all Site collections from the web application
$SitesCollection  = $WebApp.Sites

#Enumerate all site collections in the web application
Foreach($Site in $SitesCollection)
{
    #Get Site Collection URL, Owner, Content Database Details
    Write-host $Site.URL
}

While the above code is written traditionally, you can use PowerShell one-liner to get a list of all site collections in a web application:

Get-SPWebApplication "https://intranet.crescent.com" | Get-SPSite | Select URL

PowerShell to get all site collections in a web application:

This time, let’s use PowerShell expressions to dig into site collection properties, say: Site Name, content database, etc.

Get-SPWebApplication "https://intranet.crescent.com" | Get-SPSite -Limit All | Select URL,@{Name="Site Name"; Expression={$_.RootWeb.Title}},Owner 

How to get all site collections in a web application and Export to CSV?

How about getting all site collections in a web application using PowerShell and exporting to CSV?

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Web application URL and CSV File location Variables
$WebAppURL="https://intranet.crescent.com"
$CSVFile="C:\SitesList.csv"

#Get list of site collections in a web application powershell
Get-SPWebApplication $WebAppURL | Get-SPSite -Limit All | ForEach-Object {
    New-Object -TypeName PSObject -Property @{
             SiteName = $_.RootWeb.Title
             Url = $_.Url
             DatabaseName = $_.ContentDatabase.Name }
} | Export-CSV $CSVFile -NoTypeInformation

This Script gets all site collections from the given web application and exports the data to CSV.
how to get all site collections in a web application using powershell

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

7 thoughts on “Get All SharePoint Site Collections in a Web Application using PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *