Office 365 SharePoint Online Get All SubSites Under Site Collections Using PowerShell

This blog will help you to retrieve all the subsites under Site Collections in SharePoint Online, using PowerShell. Method.

Steps
  1. Open SharePoint Management Shell.
  2. Copy the code given below and paste it.
  3. Run the code. 
  1. ##########################    
  2.   
  3. #Script  : Below Script will Get All SubSites under the Site Collections  in Office 365 SharePoint Online   
  4. #Author  : Gowtham Rajamanickam    
  5. #Date    :16-03-2017  
  6. #Version:1.0    
  7.   
  8. #########################  
  9.  
  10. #Before Run the Scritp you must load the Client Assembly files   
  11.    
  12. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
  13. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
  14.      
  15.   
  16.   
  17.   
  18. $UserName= "[email protected]"  
  19. $Password = "Enter your Password here"  
  20.     
  21. $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))  
  22.    
  23. #Get the Root Web  
  24. Function Get-Subsites() {  
  25.   
  26.   
  27.     $webURL="https://gowthamr.sharepoint.com/"  
  28.     $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)  
  29.     $Context.Credentials = $credentials  
  30.     $Web = $context.Web  
  31.     $Context.Load($web)  
  32.     $Context.Load($web.Webs)    
  33.     $Context.executeQuery()  
  34.      Write-host $Web.URL  
  35.      foreach ($Subweb in $web.Webs)  
  36.     {  
  37.        Write-host $Subweb.url  
  38.     }  
  39. }  
  40.   
  41. #Call the function  
  42. Get-SubSites   
Was my blog helpful? Was any relevant content missing? If so, please let us know what's confusing or missing at the bottom of this page.

Thanks for reading my blog.
Ebook Download
View all
Learn
View all