Add-Type -Path 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll' Add-Type -Path 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll' Add-Type -Path 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll' #Gets the current directory. function Get-CurrentScriptDirectory { $Invocation = (Get-Variable MyInvocation -Scope 1).Value Split-Path $Invocation.MyCommand.Path } #Logs and prints messages function LogMessage([String] $Msg) { Write-Host $Msg -ForegroundColor Green Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") Message: $Msg" | Out-File -FilePath $currentLogPath -Append -Force } #Logs and prints messages function LogSubsite([String] $Msg) { Write-Host $Msg -ForegroundColor Cyan Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") Message: $Msg" | Out-File -FilePath $currentLogPath -Append -Force } function Get-SPOWebs(){ param([string]$url, [string]$siteCollectionTitle,[string]$siteCollectionUrl) #fill metadata information to the client context variable $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url) $clientContext.Credentials = $credentials $web = $clientContext.Web $clientContext.Load($web) $clientContext.Load($web.Webs) $clientContext.load($web.lists) try{ $clientContext.ExecuteQuery() #loop through all webs in the web and start again to find more subwebs foreach($subweb in $web.Webs) { $clientContext.Load($subweb.Lists) $clientContext.ExecuteQuery() $listCount = $subweb.lists.Count LogSubsite("Site Title : "+ $subweb.Title + " Site Url : "+ $subweb.url + " List Count : " + $listCount + " LastModifiedDate" + $subweb.LastItemModifiedDate) $props = @{'SiteCollectionTitle' = $siteCollectionTitle; 'SiteCollectionUrl' = $siteCollectionUrl; 'SiteTitle' = $subweb.Title; 'SiteUrl' = $subweb.url; 'SiteAdmin' =""; 'SiteCollectionSize' = ""; 'LastModified' = $subweb.LastItemModifiedDate ; 'ListCount' = $listCount} ; $siteitemarray = New-Object -TypeName PSObject -Property $props ; $global:siteitems += $siteitemarray Get-SPOWebs $subweb.url $siteCollectionTitle $siteCollectionUrl } } catch{ LogMessage("Could not find web " +$_.Exception.Message + $_.Exception.GetType().FullName + $_.InvocationInfo.PositionMessage) } } $currentPath = Get-CurrentScriptDirectory $currentDateTime = Get-Date -format "yyyy-MM-d.hh-mm-ss" #Path of Log File in Drive. $currentLogPath = $currentPath + "\" + "SiteAnalyticsLog_"+ $currentDateTime +".txt" # Initialize client context $adminURL = '' $username = '' $password = '' $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 $spoCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $securePassword Connect-SPOService -url $adminURL -credential $spoCredential # Enumerate all site collections   $tenantAdmin = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($clientContext)     $tenantSiteCollections = $tenantAdmin.GetSiteProperties(0, $true) $clientContext.Load($tenantSiteCollections) $clientContext.ExecuteQuery() $global:siteitems = $null $global:siteitems = @() For ($siteCount=0; $siteCount -le $tenantSiteCollections.Count; $siteCount = $siteCount+300) { if($siteCount -ne 0){ $tenantSiteCollections = $tenantAdmin.GetSiteProperties($siteCount, $true) $clientContext.Load($tenantSiteCollections) $clientContext.ExecuteQuery() } $siteNumber = 0 foreach($siteCollection in $tenantSiteCollections){ $sc = Get-SPOSite -Identity $siteCollection.Url $SizeinMB = [System.Math]::Round((($sc.StorageUsageCurrent)/1),2) $props = [Ordered]@{'SiteCollectionTitle' = $siteCollection.Title; 'SiteCollectionUrl' = $siteCollection.Url; 'SiteTitle' = ""; 'SiteUrl' = ""; 'SiteAdmin' =$sc.Owner; 'SiteCollectionSize' = $SizeinMB; 'LastModified' = $siteCollection.LastContentModifiedDate ; 'ListCount' = ""} ; $siteitemarray = New-Object -TypeName PSObject -Property $props ; $global:siteitems += $siteitemarray LogMessage("Site Title : " + $siteCollection.Title +" Site url : " + $siteCollection.Url + " LastContentModifiedDate : " + $siteCollection.LastContentModifiedDate ) Get-SPOWebs $siteCollection.Url $siteCollection.Title $siteCollection.Url $siteNumber++ } $global:siteitems | Export-Csv "SPOTenantAnalytics.csv" -Append }