Get all site collections in SharePoint using PowerShell

There might be a need to get all site collections in all the web applications in SharePoint server. We can get all web applications by using the powershell cmdlet Get-SPWebApplication and then we can get a list of site collections under a particular web application using Get-SPSite cmdlet.

Before proceed, run the following command to add SharePoint PowerShell management snapin.

Add-PSSnapin Microsoft.SharePoint.PowerShell

The following PowerShell script list all site collections in a single web application. You have to change the $url value with your own webapplication url.

$url ='http://sharepoint_server:90/'
$Site= Get-SPSite $url         
$spWebApp = $Site.WebApplication
#Enumerate all site collections from webapplication
$spWebApp.Sites | Select-Object @{n="Name"; e={$_.RootWeb.Title}},Url,ContentDatabase | FL

Get all site collections in all web applications using PowerShell

First, you need to get all web applications by using Get-SPWebApplication cmdlet and pipe the results to Get-SPSite cmdlet to get site collections for each and every web application. The following powershell script export all site collections in all web applications to CSV file.

Get-SPWebApplication | ForEach-Object {
$web = $_
$Site= Get-SPSite $web.Url -Limit All -ErrorAction SilentlyContinue       
if($Site)
{
$spWebApp = $Site.WebApplication
$spWebApp.Sites | ForEach-Object {
New-Object -TypeName PSObject -Property @{
             Name = $_.RootWeb.Title
             Url = $_.Url
             WebApplication= $web.Name
             Database = $_.ContentDatabase 
             CreationTime = $_.RootWeb.Created 
}}}} | Export-CSV "C:\AllSiteCollections.csv" -NoTypeInformation -Encoding UTF8

Advertisement

Leave a Comment