EnjoySharePoint YouTube Channel

Get Subsites of a Site Collection in SharePoint Online using PowerShell

Get Subsites in SPO using PowerShell

This PowerShell SharePoint Online tutorial explains how to get or retrieve all the subsites from a site collection in SharePoint Online Office 365.

Get Office 365 Enterprise E3 Or Business Premium Subscription & Try out all the features

Scenario:
A user wants to see the list of all the subsites in a given Site Collection in SharePoint Online. It is unfortunate that my site collection was pretty old and was filled with many subsites with multiple levels. So it is closely impossible to harvest the information manually without any slippage.


PowerShell Script to retrieve subsites from a SharePoint Online Site Collection

I have created PowerShell script to generate the information.
<# This Script is to get all the subsites in a given site collection from SPO #>

function GetSubSiteDetails {
try {
#Reading artifacts/prerequisites
$strSiteCollURL = Read-Host “Enter the Site Collection URL of SPO (eg. https://onlysharepoint2013.sharepoint.com/sites/krishna)”
$strUserId = Read-Host “Enter the username of SPO (eg. userName@OnlySharePoint2013.onmicrosoft.com)”
$strPassword = Read-Host “Please enter the password for $($strUserId)” -AsSecureString

$startedTime = Get-Date
Write-Host “Time Started = ” $startedTime
Write-Host “`nAll the Sub Sites in Site Collection $($strSiteCollURL) are::”

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path “C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Runtime.dll”

#Bind to site collection
$ObjContext = New-Object Microsoft.SharePoint.Client.ClientContext($strSiteCollURL)
$ObjCreds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($strUserId,$strPassword)
$ObjContext.Credentials = $ObjCreds



#Iterating through the Site Collections
Get-SPOWebs($strSiteCollURL)

$completedTime = Get-Date
Write-Host “`nTime Completed = ” $completedTime
Write-Host “Script Excecuted Successfully !!!”
}
catch {
write-host “Error: $($_.Exception.Message)” -foregroundcolor red
}
}
function Get-SPOWebs($url) {
#Creating object to Site Collection
$objClientCntx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$objClientCntx.Credentials = $ObjCreds
$objWeb = $objClientCntx.Web
$objClientCntx.Load($objWeb)
$objClientCntx.Load($objWeb.Webs)
try {
$objClientCntx.ExecuteQuery()
#Iterate through all the subsites in a site collection
foreach ($objWeb in $objWeb.Webs) {
write-host “Site Name::$($objWeb.Title)`t`t Site URL :: $($objWeb.url)” -foregroundcolor green
Get-SPOWebs($objWeb.url)
}
}
catch {
write-host “Could not find web” -foregroundcolor red
}
}
GetSubSiteDetails
<# To run this script you need to set the PowerShell Command-let execution policies as follows
Check the execution policies
Get-ExecutionPolicy -List
Step 2 Set the execution policy to “RemoteSigned” with the below command
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Step 3 Execute the PS1
Step 4 remove the execution policies
Set-ExecutionPolicy Undefined -Scope LocalMachine
#>

The above script has made my life simply and smooth. The output will be as follows:

Get Subsites in SPO using PowerShell
Get Subsites in SPO using PowerShell

You may like following PowerShell SharePoint tutorials:

Hope this PowerShell SharePoint Online tutorial explains how to retrieve all sub sites under a site collection using PowerShell in SharePoint Online Office 365.

Check out Best Alternative to InfoPath -> Try Now
You May Also like the Following SharePoint Tutorials:

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →