How to retrieve all the Site Collections along with its subsites from SharePoint online using PowerShell. Please suggest.


1 Answer 1


Here is the complete script to get all sites in a site collection from SharePoint Online:

Add-Type -Path "c:\folder\Microsoft.SharePoint.Client.dll"

Add-Type -Path "c:\folder\Microsoft.SharePoint.Client.Runtime.dll"

$siteUrl = "https://mytenant.sharepoint.com/sites/mysitecollection"

$password = Read-Host -Prompt "Enter password" -AsSecureString

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("admin@mytenant.onmicrosoft.com", $password)

$ctx.Credentials = $credentials

$rootWeb = $ctx.Web

$sites = $rootWeb.Webs




foreach($site in $sites)




  Write-Host $site.Title "-" $site.Url


Source: http://blogs.technet.com/b/heyscriptingguy/archive/2014/04/24/create-and-manage-sharepoint-online-sites-by-using-powershell.aspx

  • Hi, Thanks for the information. Is there a way to get all the sitecollections initially and then loop through it to get its subsites? As am new to powershell, i do not have much idea
    – user38433
    Mar 11, 2015 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.