Definitively, the possibilities provided by PowerShell and the SharePoint Client Side Object Model to interact with a SharePoint Online tenant are awesome. As a proof of this statement, this time I’m sharing a new script where I show how easy is to get all the site collections in a SharePoint Online tenant using the CSOM and PowerShell. As you can see in the script (you can download it from How to get all the tenant site collections using CSOM and PowerShell!, thanks to the Microsoft.Online.SharePoint.Client.Tenant I can use the Tenant object to get all the site collections in the tenant by calling the GetSiteProperties() method defined in the Tenant class. I hope you find this scrip helpful.
1: ############################################################################################################################################
2: #Script that allows to get the site collections in a SPO Tenant using CSOM
3: # Required Parameters:
4: # -> $sUserName: User Name to connect to the SharePoint Online Site Collection.
5: # -> $sPassword: Password for the user.
6: # -> $sSiteUrl: SharePoint Online Administration Url.
7: ############################################################################################################################################
8:
9: $host.Runspace.ThreadOptions = "ReuseThread"
10:
11: #Definition of the function that gets the list of site collections in the tenant using CSOM
12: function Get-SPOTenantSiteCollections
13: {
14: param ($sSiteUrl,$sUserName,$sPassword)
15: try
16: {
17: Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
18: Write-Host "Getting the Tenant Site Collections" -foregroundcolor Green
19: Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
20:
21: #Adding the Client OM Assemblies
22: Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"
23: Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"
24: Add-Type -Path "<CSOM_Path>\Microsoft.Online.SharePoint.Client.Tenant.dll"
25:
26: #SPO Client Object Model Context
27: $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
28: $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)
29: $spoCtx.Credentials = $spoCredentials
30: $spoTenant= New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($spoCtx)
31: $spoTenantSiteCollections=$spoTenant.GetSiteProperties(0,$true)
32: $spoCtx.Load($spoTenantSiteCollections)
33: $spoCtx.ExecuteQuery()
34:
35: #We need to iterate through the $spoTenantSiteCollections object to get the information of each individual Site Collection
36: foreach($spoSiteCollection in $spoTenantSiteCollections){
37:
38: Write-Host "Url: " $spoSiteCollection.Url " - Template: " $spoSiteCollection.Template " - Owner: " $spoSiteCollection.Owner
39: }
40: $spoCtx.Dispose()
41: }
42: catch [System.Exception]
43: {
44: write-host -f red $_.Exception.ToString()
45: }
46: }
47:
48: #Required Parameters
49: $sSiteUrl = "https://<YourOffice365Domain>-admin.sharepoint.com/"
50: $sUserName = "<SPO_Administration_User>"
51: #$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString
52: $sPassword=convertto-securestring "<User_Password>" -asplaintext -force
53:
54: Get-SPOTenantSiteCollections -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword
And this is the result you will get after executing the script: