SharePoint Online: Get Subsites using PowerShell

Requirement: Get a subsite using PowerShell in SharePoint Online

PowerShell to Get Subsites in SharePoint Online

This blog post shows how you can get a subsite in SharePoint Online using PowerShell. This script can be useful if you need to quickly perform some action on a specific subsite or gather information about a particular site.

Get a Subsite using PowerShell in SharePoint Online

As there is no PowerShell cmdlet to retrieve SharePoint Online subsite through CSOM, Here is how to get a subsite (spweb) from PowerShell in SharePoint Online:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Function to Get the Web
Function Get-SPOWeb() 
{
    Param
    (
        $WebURL = $(throw "Please Enter the Site URL:")
    )
    Try {
        #sharepoint online powershell get web
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        $Web = $Ctx.Web
        $Ctx.Load($web)
        $Ctx.executeQuery()
        Return $Web 
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Get Credentials to connect
$Cred = Get-Credential

#powershell cmdlet to get sharepoint online subsite
$Web = Get-SPOWeb -WebURL "https://crescent.sharepoint.com/sites/marketing"

#sharepoint online powershell get site title
Write-host $Web.Title

This is equivalent to Get-SPWeb cmdlet in SharePoint On-premises PowerShell.

PnP PowerShell cmdlet to get SharePoint Online subsite

Getting a SharePoint Online subsite from PnP PowerShell is pretty simple:

#Parameters
$SiteURL ="https://Crescent.sharepoint.com/sites/marketing/2018"

#Connect to PnP Online
Connect-PnPOnline -URL $SiteURL -Interactive 

#Get the subsite
$Subsite = Get-PnPWeb

#Get Subsite title
Write-Host $Subsite.Title

SharePoint Online: PowerShell to Get All Subsites of a site

Similarly, we can get subsites of a given site using PowerShell as:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Function to Get Subsites of the Web
Function Get-SPOSubSites() 
{
    Param
    (
        $WebURL = $(throw "Please Enter the Site URL:")
    )
    Try {
        #Get subsites of a site (1st Level or immediate childs)
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        $Web = $Ctx.Web
        $Ctx.Load($web)
        $Ctx.Load($web.Webs)
        $Ctx.executeQuery()
        Return $Web.Webs
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Get Credentials to connect
$Cred = Get-Credential

#Get All Subsites of the Web
$SubSites = Get-SPOSubSites -WebURL "https://crescent.sharepoint.com/sites/marketing"

#Get the URL of Each subsite
ForEach($Site in $SubSites)
{
    Write-host $Site.Url
}

Getting all subsites from a SharePoint Online site using PnP PowerShell is easy:

#Parameters
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -URL $SiteURL -Interactive 

#Get subsites of the site
Get-PnPSubWeb

This script returns all immediate subsites of a given site. How about getting all subsites recursively?

SharePoint Online: Get All Subsites Recursively using PowerShell

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Function to Get all Subsites recursively
Function Get-SPOSubSitesRecursively() 
{
    Param
    (
        $WebURL = $(throw "Please Enter the Site URL:")
    )
    Try {
        $SubSites= @()
        #Get subsites of a site
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Webs)
        $Ctx.executeQuery()
        $SubSites += $Web

        #Call the function recursively on each subsite
        ForEach($Web in $Web.Webs)
        {
            $SubSites += Get-SPOSubSitesRecursively $Web.Url
        }
        Return $SubSites
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Get Credentials to connect
$Cred = Get-Credential

#Get All Subsites of the Web
$SubSites = Get-SPOSubSitesRecursively -WebURL "https://crescent.sharepoint.com/sites/marketing"

#Get the URL of Each subsite
ForEach($Site in $SubSites)
{
    Write-host $Site.Url
}

The same can be done with PnP PowerShell as:

#Parameters
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -URL $SiteURL -Interactive 

#Get all subsites of the site recursively
Get-PnPSubWeb -Recurse

If you want to include the Root site as well, use: “-IncludeRootWeb” switch:

Get-PnPSubWeb -Recurse -IncludeRootWeb

Related Posts:

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply

Your email address will not be published.