8

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?

1
  • 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
    Commented Sep 19, 2019 at 14:55

3 Answers 3

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 = "[email protected]"
$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 = "[email protected]"
$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 '-----------------------------' 
}    
6
  • Thanks Vadim but I'm getting this error. Get-SPOWebs : The term 'Get-SPOWebs' is not recognized as the name of a cmdlet,
    – vzion
    Commented May 30, 2014 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 Commented May 30, 2014 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
    Commented May 30, 2014 at 21:58
  • @VadimGremyachev Is this doable through an API? REST API? Commented Jul 29, 2015 at 11:59
  • @SyedMauzeRehan, i think it is doable via REST API, sounds like a good question ;) Commented Jul 29, 2015 at 12:06
7

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.

0

You can use the Microsoft Graph PowerShell cmdlets to List all available sites in an organization using the Graph API.

After you connect to the Graph API using Connect-MgGraph with the permissions required to list all available sites in an organization, you can use

$More = $true
$uri = "https://graph.microsoft.com/v1.0/sites?  
$select=siteCollection,webUrl&$filter=siteCollection/root%20ne%20null"
$siteCollections = @()
$reqResult = Invoke-MgGraphRequest -Method GET $uri
While ( $More ) {
  $siteCollections += $reqResult.Value
  $siteCollections.Count
  If ( $reqResult.ContainsKey( '@odata.nextLink' ) ) { 
    $reqResult = Invoke-MgGraphRequest -Uri $reqResult.'@odata.nextLink' -Method GET 
  } else { $More = $false }
}

to read a list of available sites into the variable $siteCollections

Please note: depending on the amount of sites that are available, that might take minutes or hours.
After that, you can display the sites using

$siteCollection | Out-Gridview

or write the URLs of the sites to a file

$siteCollection.GetEnumerator().webUrl | out-file -LiteralPath "webUrls.txt"

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