Is there someone who created a working script for SPOnline that lists in Excel compatible the URL of all subsites in a site collection?

Something like this


1 Answer 1


This should do the trick - just call the last two lines for each site collection, I usually do it with a CSV list of site collection URLs from another command (can't remember the SPO one to get all site collection URLs, but it's the equivalent of Get-SpSite in server side OM) piped into a for..each loop.

Note you need to run this in a shell where you've imported the Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime DLLs - if you don't have these, download them as part of the 'SharePoint Online Client Components SDK' and add lines to the top using Add-Type with the path to the DLLs.

function Get-SPOWebs(){ 
   $Url = $(throw "Please provide a Site Collection Url"), 
   $Credential = $(throw "Please provide a Credentials") 

  $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)   
  $context.Credentials = $Credential  
  $web = $context.Web 
  foreach($web in $web.Webs) 
       Get-SPOWebs -Url $web.Url -Credential $Credential  
       $web >> path\to\output\file

$UserName = "yourusername@tenant" 
$Password = "yourpassword" 
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force 
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword) 

$AllWebs = Get-SPOWebs -Url 'https://tenant/site/collection' -Credential $SPOCredentials 
$AllWebs | %{ Write-Host $_.Title }
  • About your second paragraph: I was thinking of using the SharePoint online management shell. Is that ok or I still have to add those DLL?
    – susan
    Commented Aug 23, 2017 at 12:42
  • It should work with just the SPO shell but if not, then add the DLLs. Commented Aug 23, 2017 at 12:42
  • I got errors, should I use windows powershell ISe to be able to write all of that?
    – susan
    Commented Aug 24, 2017 at 11:48
  • the portion for authentication needs to be amended for accounts who have the multifactor authentication
    – susan
    Commented Aug 24, 2017 at 14:25
  • Or seeing as you only need to run it as one account, just use one without MFA enabled. I don't deal with any tenants that use MFA, personally I think it's just a pain. Would appreciate it if you mark this as the answer as it does what was asked. Commented Aug 25, 2017 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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