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! Use the Get-PnPWeb PowerShell cmdlet to get SharePoint Online subsite.

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

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

#Get the Web
$Web = Get-PnPWeb

#Get the title
Write-Host $Web.Title

This cmdlet will return the web (or subsite) for a given SharePoint Online site, making it easy to work with the information you need. Just specify the URL of the site you want to get and the cmdlet will do the rest.

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

To get all the properties of a subsite, use the following command: Get-PnPSubweb -Identity “Subsite-URL” | Select-Object -Property *. This will return all the properties of the given subsite. To get specific information about a property, use the following command: Get-PnPSubweb -Identity “Subsite-URL” | Select-Object -ExpandProperty Title. This will return the value of the Title property for the given subsite.

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
}

To get a list of all subsites in SharePoint Online, you can use the Get-PnPSubweb PnP PowerShell cmdlet. This cmdlet will return a list of all subsites from the specified site collection. You can also use the -Recurse switch to get a list of all subsites from all child sites.

#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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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