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 know about "Get-SPWebApplication" but it's not available in Online.
How can I get a list of all sites and subsites in SPO?
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
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 }
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 '-----------------------------'
}
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:
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.
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"