Find All OneDrive Site Collections in SharePoint Online using PowerShell

Requirement: Get a list of all OneDrive site collections from SharePoint Online.

I was asked to get the list of all OneDrive sites to get the usage of OneDrive for Business in our Office 365 tenant. Its bit tricky to find all of your OneDrive site collections and here is how I achieved using Get-SPOSite cmdlet:

Find All OneDrive Sites using SharePoint Online Management Shell:
Here is the PowerShell script to get all OneDrive site collections in SharePoint Online.

#Get Credentials to connect to SharePoint Admin Center
$Cred = Get-Credential

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL –credential $Cred

#Get all Personal Site collections and export to a Text file
Get-SPOSite -Template "SPSPERS" -limit ALL -includepersonalsite $True | Select URL, Owner | Format-table -autosize | Out-string -width 8096 | Out-file C:\temp\OneDriveSites.txt 
Just change the $AdminSiteURL to your domain, supply the credentials for SharePoint Online administrator and run the script. This script produces the output on "C:\Temp\OneDriveSites.txt" file.
get all onedrive site collections using powershell

Get All OneDrive Sites using PowerShell - CSOM:
As an alternate approach, you can use PowerShell CSOM to get all OneDrive site collections. This script fetches all SharePoint Online users using UserProfileService and checks if the user has Personal site. Here is the script:
#Add references to SharePoint client assemblies

# Specify sharepoint online admin url 
$AdminSiteURL = ""

#Specify the location where the list of OneDrive sites should be saved
$LogFile = "C:\Temp\OneDriveSites.txt"
#Setup Credentials to connect
$AdminCred= Get-Credential
$AdminCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminCred.Username, $AdminCred.Password)
#Set User Profile Service path using SPO admin URL 
$ProxyAddr = "$AdminSiteURL/_vti_bin/UserProfileService.asmx?wsdl"
# Create a new webservice proxy to access UserProfileService
$UserProfileService= New-WebServiceProxy -Uri $ProxyAddr -UseDefaultCredential False
$UserProfileService.Credentials = $AdminCredentials
#Set authentication cookies
$StrAuthCookie = $AdminCredentials.GetAuthenticationCookie($AdminSiteURL)
$URI = New-Object System.Uri($AdminSiteURL)
$Container = New-Object System.Net.CookieContainer
$Container.SetCookies($URI, $StrAuthCookie)
$UserProfileService.CookieContainer = $Container
# Sets the first User profile, at index -1
$UserProfileResult = $UserProfileService.GetUserProfileByIndex(-1)
$NumProfiles = $UserProfileService.GetUserProfileCount()
$i = 1
# As long as the next User profile is NOT the one we started with (at -1)...
While ($UserProfileResult.NextValue -ne -1) 
    Write-Host "Checking profile $i of $NumProfiles" -foreground Yellow
    # Look for the Personal Space object in the User Profile and retrieve it  
    $Prop = $UserProfileResult.UserProfile | Where-Object { $_.Name -eq "PersonalSpace" } 
    $URL= $Prop.Values[0].Value
    # If "PersonalSpace" (which we've copied to $Url) exists, log it to our file...
    if ($URL) 
        $SiteUrl = $MySiteURL+$URL
        # Write OneDrive site url to the console
        Write-Host $SiteUrl -foreground Green
        $SiteUrl | Out-File $LogFile -Append -Force
    # And now we check the next profile the same way...
    $UserProfileResult = $UserProfileService.GetUserProfileByIndex($UserProfileResult.NextValue)
Find All OneDrive Site Collections in SharePoint Online using PowerShell Find All OneDrive Site Collections in SharePoint Online using PowerShell Reviewed by Salaudeen Rajack on April 23, 2018 Rating: 5


  1. Its really very helpful, Thank you so much!

  2. I get an empty text file after running script. I know we have OneDrive for Business where we have 100’s of OneDrives setup for users.

    1. Looks like you are running older version of SharePoint Online Management Shell. You can download and install the latest from:

  3. I Wasted days trying different suggestions before finding your post! Thank you so much.

  4. Thank you for this script, it's super useful.


Please Login and comment to get your questions answered!

Powered by Blogger.