SharePoint Online: PowerShell to Get All Subsites in a Site Collection

Requirement: 
PowerShell to list all subsites in a SharePoint Online Site collection!
sharepoint online powershell get all subsites

SharePoint Online: Get All Subsites using PowerShell
#Requirement: sharepoint online get all subsites 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"

#Variables
$SiteURL = "https://crescent.sharepoint.com/Sites/Marketing"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the web
    $Web = $Ctx.web
    $Ctx.Load($Web)
    
    #get all subsites in sharepoint online powershell
    $Ctx.Load($Web.Webs)
    $Ctx.executeQuery()

    #Loop through each subsite and get subsite URL
    ForEach ($Subweb in $web.Webs)  
    {
       Write-host $Subweb.url  
    }  
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
This PowerShell script gets all immediate subsites of a given site. How to list all subsites recursively?

PowerShell to Get all sub-sites in a Site Collection:
Here is the SharePoint online PowerShell to get all subsites (Webs) recursively.
#Load SharePoint Online 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"
  
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$UserName="Salaudeen@crescent.com"
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#Get the Root Web
Function Get-SPOWeb() {
param(
    $WebURL = $(throw "Please Enter the Site Collection URL")
)

    #Get Web information and subsites
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
    $Context.Credentials = $credentials
    $Web = $context.Web
    $Context.Load($web)
    $Context.Load($web.Webs)
 
    $Context.executeQuery()

    #Do something with the current sub-site
    Write-host $Web.URL

    #Iterate through each subsite in the current web
    foreach ($Subweb in $web.Webs)
    {
        #Call the function recursively to process all subsites underneaththe current web
        Get-SPOWeb($Subweb.url)
    }
}

#Call the function
Get-SPOWeb -WebURL $SiteUrl
This PowerShell gets all subsites from a given site collection. Here is my another article which iterates through all subsites and changes the logo: SharePoint Online: How to Change Site Logo using PowerShell?

SharePoint Online: PowerShell to List All Subsites in All Site Collections
Lets get subsites from all site collections in the tenant.
#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 each subsite in site collection
Function Get-SPOWebs($SiteURL)
{
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.Webs)
    $Ctx.ExecuteQuery()

    #Do something
    Write-host $Web.Url

    #Loop through each each subsite in site
    Foreach($Web in $web.Webs)
    {
        Get-SPOWebs $Web.Url
    }
}

#Variables
$AdminCenterURL = "https://crescenttech-admin.sharepoint.com"

#Setup Credentials to connect
$Cred= Get-Credential
 
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential ($Cred)

#Get all Site collections
$Sites = Get-SPOSite -Limit All

#Loop through site collections
ForEach($Site in $Sites)
{
    #Call the function to get all sub-sites in site collection
    Get-SPOWebs($Site.URL)
}
To get all sites and subsites in SharePoint Online using PowerShell, use: http://www.sharepointdiary.com/2017/05/get-all-site-collections-and-subsites-in-sharepoint-online-powershell.html

Get Sub sites of a Site using PowerShell PnP
Here is the PnP PowerShell to get all subsites in site collection in SharePoint Online
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get All Subsies of the given site URL
    $WebsCollection = Get-PnPSubWebs -Recurse

    ForEach($Web in $WebsCollection)
    {
        Write-host $Web.Url
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
The -Recurse switch in Get-PnPSubWebs cmdlet gets all subsites from all levels of the site collection recursively.
SharePoint Online: PowerShell to Get All Subsites in a Site Collection SharePoint Online: PowerShell to Get All Subsites in a Site Collection Reviewed by Salaudeen Rajack on February 27, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.