The below sample Power Shell script will retrieve all its Site Collections and its sub sites.
Command to Execute
.\<<File Name>>.ps1 <<WebApplicationURL>>
Ex
.\DisplayAllSites.ps1 “http://RKWebApplication.com”
Power Shell Script
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Publishing")
[System.Reflection.Assembly]::LoadWithPartialName("System")
[System.Reflection.Assembly]::LoadWithPartialName("System.IO")
$PSshell = Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorVariable err -ErrorAction SilentlyContinue
if($PSshell -eq $null){ Add-PSSnapin "Microsoft.SharePoint.PowerShell" }
function DisplayAllSiteCollectionsAndWebs([string]$applicationUrl)
{
Write-Host "WEB APPLICATION URL: $($applicationUrl)"
try
{
# Getting WebApplication based on url
$rootSite = New-Object Microsoft.SharePoint.SPSite($applicationUrl)
$spWebApp = $rootSite.WebApplication
$spSiteColl = $spWebApp.Sites
# Looping Site Collections
For($i=0; $i -lt $spSiteColl.Count; $i++)
{
# Site Collection URL
$SiteCollURL = $spSiteColl[$i].URL
Write-Host "`t- Site Collection URL: $($SiteCollURL)"
$allwebs = $spSiteColl[$i].AllWebs
# Looping all Webs(sites)
For($j=0; $j -lt $allwebs.Count; $j++)
{
$webURL = $allwebs[$j].URL
Write-Host "`t`t- Web URL: $($webURL)"
}
}
$rootSite.Dispose();
}
catch [system.exception]
{
Write-Host "[ERROR] - Exception: ($($_.Exception.Message))"
}
}
Write-Host "------------- Start of the Script -------------"
$webApplicationUrl = $args[0]
DisplayAllSiteCollectionsAndWebs $webApplicationUrl
Write-Host "--------------- End of the Script -------------"
Output
--------------- Start of the Script -------------
WEB APPLICATION URL: http://RKWebApplication.com
- Site Collection URL: http://RKWebApplication.com/BRK_SiteCollection
- Web URL: http://RKWebApplication.com/BRK_SiteCollection
- Web URL: http://RKWebApplication.com/BRK_SiteCollection/RKSubSite
- Web URL: http://RKWebApplication.com/BRK_SiteCollection/RKSubSite2
- Site Collection URL: http://RKWebApplication.com/Rama_SiteCollection
- Web URL: http://RKWebApplication.com/Rama_SiteCollection
- Web URL: http://RKWebApplication.com/Rama_SiteCollection/RamaSubSite
- Web URL: http://RKWebApplication.com/Rama_SiteCollection/RamaSubSite2
--------------- End of the Script -------------