kwizcom banner advertisement

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