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

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

        #Call the function recursively to process all subsites underneath the current web

#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 my other posts to get all site collections and subsites individually:

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 = ""

#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
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.