SharePoint Online: PowerShell to Get All Site Collections and Subsites

Requirement: SharePoint Online PowerShell to get all site collections and subsites.

SharePoint Online PowerShell to list all site collections and subsites
Here is the PowerShell to list all sites and subsites in SharePoint Online.
Import-Module Microsoft.Online.SharePoint.Powershell

#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"

#Get Credentials to connect to SharePoint Admin Center
$Cred = Get-Credential

#Function to get all subsites of a site
Function Get-SPOWeb($WebURL)
{ 
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #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()
 
    #Iterate through each subsite in the current web
    foreach ($Subweb in $web.Webs)
    {
        #Get the web object
        $Subweb

        #Call the function recursively to process all subsites underneath the current web
        Get-SPOWeb($Subweb.url)
    }
}

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL –Credential $Cred

#Get All site collections
$SiteCollections = Get-SPOSite -Limit All

#Traverse through each site collection and get their subsits
Foreach ($Site in $SiteCollections)
{
    Write-Host $Site.Url     
    $AllWebs = Get-SPOWeb $Site.Url
    $AllWebs | ForEach-Object { Write-Host $_.URL }
}    
    
This gets you the list of site collections and their subsites. Here is the another script:

SharePoint Online: PowerShell to List All Subsites in All Site Collections
Lets get subsites from all site collections from the tenant. Here is the PowerShell script to get all site collections and subsites 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 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)
}

PnP PowerShell to Get All Sites and Subsites in SharePoint Online
Let's retrieve all site collections and their subsites recursively using PnP PowerShell
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/"

#Get Credentials to connect
$Cred = Get-Credential

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

    #Get All Site collections  
    $SitesCollection = Get-PnPTenantSite

    #Loop through each site collection
    ForEach($Site in $SitesCollection)  
    {  
        Write-host -F Green $Site.Url  
        Try {
            #Connect to site collection
            Connect-PnPOnline -Url $Site.Url -Credentials $Cred

            #Get Site Collection administrators of the site
            $SubSites = Get-PnPSubWebs -Recurse
            ForEach ($web in $SubSites)
            {
                Write-host `t $Web.URL
            }
        }
        Catch {
            write-host -f Red "`tError:" $_.Exception.Message
        }
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
This script gets all sites and subsites in SharePoint online using PnP PowerShell
Make sure you have Site collection Administrator rights on all site collections in the tenant before executing this script. Otherwise, you may get "The remote server returned an error: (401) Unauthorized." error! PowerShell to Add Site Collection Administrator to All Sites in SharePoint Online

Related Posts:
SharePoint Online: PowerShell to Get All Site Collections and Subsites SharePoint Online: PowerShell to Get All Site Collections and Subsites Reviewed by Salaudeen Rajack on May 23, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.