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),Template`r`n"
$sites = Get-SPOSite -Limit all | Select Url,SharingCapability,StorageQuota,Template
foreach($site in $sites){
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($site.Url)  
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
        $web = $context.Web 
        $title = $web.Title
    catch {
        $title = "Error fetching title"
        $csv +=$title + "," + $site.Url + "," + $site.SharingCapability + "," + $site.StorageQuota + "," + $site.Template
$fso = new-object -comobject scripting.filesystemobject
$file = $fso.CreateTextFile($path,$true)

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.

  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Dangph
    Jan 20, 2016 at 10:59
  • \$\begingroup\$ 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. \$\endgroup\$ Jan 20, 2016 at 12:00
  • \$\begingroup\$ 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? \$\endgroup\$
    – Dangph
    Jan 20, 2016 at 17:07
  • \$\begingroup\$ We have about 100 SPO sites, and doing a simple CSOM fetch for the title shouldn't be as time consuming as it is. \$\endgroup\$ Jan 20, 2016 at 17:47

1 Answer 1


Well, I cannot tell why it's so slow, but this should solve your problem:

$sites = Get-SPOSite -Limit all -Detailed

-Detailed switch will cause Get-SPOSite to load all properties. It makes iterating all sites to get title unnecessary.

  • \$\begingroup\$ Unlike some other Stack Exchange sites, we like to know why some code you be better versus what the owner have. We are here to help the OP code better and explaining things are a good way to help the OP. \$\endgroup\$
    – Marc-Andre
    Feb 11, 2016 at 20:20
  • \$\begingroup\$ I totally did not see the -Detailed switch, that gets me everything I need plus allows me to pull other fields that normally aren't calculated like StorageUsageCurrent. \$\endgroup\$ Feb 11, 2016 at 21:42
  • \$\begingroup\$ @Marc-Andre This one seemed quite self explanatory, but you're right. I've added short explanation and I'll keep that in mind next time. \$\endgroup\$ Feb 12, 2016 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.