7

I know about "Get-SPWebApplication" but it's not available in Online.

How can I get a list of all sites and subsites in SPO?

  • i am having a problem running this script. My O365 tenant has MFA enabled, so i cannot exaclty enter my username and password as i need to enter a token. I tried to enter the Connect-SPOService cmdlet, but not luck. Can someone please show me how to use with a MFA enabled account and also how to export the results to csv. Thanks. – user35267 Sep 19 '19 at 14:55
13

Using SharePoint Online Management Shell that contains Get-SPOSite cmdlet you could retrieve information about site collections as demonstrated below for SharePoint Online (SPO).

Example:

Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$AdminUrl = "https://tenant-admin.sharepoint.com/"
$UserName = "username@tenant.onmicrosoft.com"
$Password = "password"
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $SecurePassword

#Retrieve all site collection infos
Connect-SPOService -Url $AdminUrl -Credential $Credentials
$sites = Get-SPOSite 

How to retrieve all sites via CSOM in PowerShell

But since SharePoint Online Management Shell does not contain any cmdlets for working with sites, we will utilize CSOM API for that purpose (REST API could also be used). The below function retrieves all the sites in a site collection:

function Get-SPOWebs(){
param(
   $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
  $context.Load($web)
  $context.Load($web.Webs)
  $context.ExecuteQuery()
  foreach($web in $web.Webs)
  {
       Get-SPOWebs -Url $web.Url -Credential $Credential 
       $web
  }
}

Example: print all sites for a site collection in SPO

Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 


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

$AllWebs = Get-SPOWebs -Url 'https://tenant.sharepoint.com' -Credential $SPOCredentials
$AllWebs | %{ Write-Host $_.Title }

Result

By combining both techniques you could achieve the desired results:

#Retrieve all site collection infos
Connect-SPOService -Url $AdminUrl -Credential $Credentials
$sitesInfo = Get-SPOSite 

#Retrieve and print all sites
foreach ($site in $sitesInfo)
{
    Write-Host 'Site collection:' $site.Url     
    $AllWebs = Get-SPOWebs -Url $site.Url -Credential $SPOCredentials
    $AllWebs | %{ Write-Host $_.Title }   
    Write-Host '-----------------------------' 
}    
| improve this answer | |
  • Thanks Vadim but I'm getting this error. Get-SPOWebs : The term 'Get-SPOWebs' is not recognized as the name of a cmdlet, – vzion May 30 '14 at 19:50
  • This error occurs since Get-SPOWebs is a custom function. You could download the complete script from here gist.github.com/vgrem/9763560fc9a5fd273ea0 – Vadim Gremyachev May 30 '14 at 20:14
  • Hi Vadim, Thanks it worked. It will list all subsites under all site collections. But say if Site A has 10 subsites, I don't know which site it belongs. Can they be grouped together? – vzion May 30 '14 at 21:58
  • @VadimGremyachev Is this doable through an API? REST API? – Syed Mauze Rehan Jul 29 '15 at 11:59
  • @SyedMauzeRehan, i think it is doable via REST API, sounds like a good question ;) – Vadim Gremyachev Jul 29 '15 at 12:06
6

Powershell for SharePoint Online is very limited today with only 30 cmdlets available compared to 774 for SharePoint Server. The only site collection cmdlets you can use are these:

  • Get-SPODeletedSite
  • Get-SPOSite
  • New-SPOSite
  • Remove-SPODeletedSite
  • Remove-SPOSite
  • Repair-SPOSite
  • Restore-SPODeletedSite
  • Set-SPOSite
  • Test-SPOSite

Introduction to the SharePoint Online Management Shell

To iterate all sites and sub sites - you need to use different techniques, such as PowerShell and CSOM or Office 365 REST API.

| improve this answer | |

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.