In previous post, I have written C# code sample to get the list of all site collections in SharePoint Online using SharePoint’s client object model (CSOM). In this article, I am going to write PowerShell script sample to retrieve all site collections in a SharePoint tenant.
Before proceed, you need to download and install the SharePoint Server 2013 Client Components SDK
The below powershell script get all sites collection from the given SharePoint tenant. You have to provide your own tenant url, admin username and password.
[Reflection.Assembly]::LoadFrom("C:\SharepointCSOMMicrosoft.SharePoint.Client.dll") [Reflection.Assembly]::LoadFrom("C:\SharepointCSOMMicrosoft.Online.SharePoint.Client.Tenant.dll") # Initialize client context $adminURL = 'https://MyTenant-admin.sharepoint.com/' $username = '[email protected]' $password = 'MyPassword' $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($adminURL) $securePassword = ConvertTo-SecureString $password -AsPlainText -Force $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$securePassword) $clientContext.Credentials = $credentials # Enumerate all site collections $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($clientContext) $props = $tenant.GetSiteProperties(0, $true) $clientContext.Load($props) $clientContext.ExecuteQuery() foreach($sp in $props) { Write-Host 'Title:' $sp.Title Write-Host 'Url:' $sp.Url Write-Host '-----------------' }
Advertisement
Why not use the cmdlets instead? Way less code..
$cred = Get-Credentials
Connect-SPOService -url https://contoso-admin.sharepoint.com -Credential $credential
Get-SPOSite | Select Title, Url
(don't forget the '-admin' in the url!)
thanks ralph