# SharePoint Online inventory

I'm creating a script to turn into a commandlet we can reuse as part of our custom PowerShell module. This works, but is very slow. I don't suspect it is anything to do with my code. I have to individually load each site to retrieve the title even though this property is visible off the $site object, it is always empty/null. Anything else I can do to improve the efficiency?$path = "C:\Users\$env:USERNAME\Desktop\SPOExport.csv"$csv = "Title,URL,Sharing Capability,Storage Quota (MB),Templatern"
$sites = Get-SPOSite -Limit all | Select Url,SharingCapability,StorageQuota,Template foreach($site in $sites){ try{$context = New-Object Microsoft.SharePoint.Client.ClientContext($site.Url)$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName,$credential.Password)
$web =$context.Web
$context.Load($web)
$context.ExecuteQuery()$title = $web.Title } catch {$title = "Error fetching title"
}
finally{
$csv +=$title + "," + $site.Url + "," +$site.SharingCapability + "," + $site.StorageQuota + "," +$site.Template
}
}
$fso = new-object -comobject scripting.filesystemobject$file = $fso.CreateTextFile($path,$true)$file.write($csv)$file.close()

Note that I have not fully adapted it into a commandlet yet, which will have all the commenting and help text available, I'm more worried about the code and the time it takes for this operation presently.

• Which part is slow? I'm not sure how to do proper profiling in PowerShell, but for a poor-man's profiler, you could just single-step through the script in the PowerShell ISE and see if any lines stand out as being slow. – Dangph Jan 20 '16 at 10:59
• The try block is very slow, I don't know if it is us being throttled by Microsoft or what, but it was taking upwards of 20 minutes to get through 20% of our SharePoint Online sites. – Eric Alexander Jan 20 '16 at 12:00
• There are 6 lines in the try block. Is any one of them particularly slow? If you want to speed something up, it's helpful to know exactly where the slowness is. How many sites are there? – Dangph Jan 20 '16 at 17:07
• We have about 100 SPO sites, and doing a simple CSOM fetch for the title shouldn't be as time consuming as it is. – Eric Alexander Jan 20 '16 at 17:47