How to retrieve all the Site Collections along with its subsites from SharePoint online using PowerShell. Please suggest.
-
try this one: stackoverflow.com/questions/22604076/…– Waqas Sarwar MVP ♦Mar 9, 2015 at 13:32
1 Answer
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
$ctx.Load($rootWeb)
$ctx.Load($sites)
$ctx.ExecuteQuery()
foreach($site in $sites)
{
$ctx.Load($site)
$ctx.ExecuteQuery()
Write-Host $site.Title "-" $site.Url
}
-
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 Mar 11, 2015 at 7:56