Find All OneDrive for Business 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. It's a 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:
OneDrive for Business provides centralized storage to save and share files. Files stored in OneDrive can be downloaded on any device and synchronized to the cloud to ensure data consistency. Let's find all OneDrive site collections in SharePoint Online using PowerShell. This PowerShell script gets all OneDrive site collections in SharePoint Online by filtering site template property.

#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

List All OneDrive for business sites using PowerShell You can also use -Filter You can also use -Filter switch in Get-SPOSite cmdlet as:
Get-SPOSite -IncludePersonalSite $true -Limit all -Filter "Url -like ''"

Get All OneDrive Sites using PnP PowerShell
We get retrieve the list of all OneDrive site collections using PnP PowerShell as:
#Config Variables
$TenantSiteURL =  ""
#Connect to PnP Online
Connect-PnPOnline -Url $TenantSiteURL -UseWebLogin

#Get All OneDrive Sites
Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like ''"

Get All OneDrive Sites using PowerShell - CSOM:
As an alternative 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 a Personal site. Here is the script to list all OneDrive for business sites with PowerShell:
#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)


  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.