Office 365 - Identify how much storage a OneDrive site is consuming with PowerShell

UPDATE: It now appears to be possible to use Get-SPOSite to bind to a OneDrive site (I'm sure that this didn't work before!).

I recently helped a colleague to write a script that reports how much storage a OneDrive site is consuming, for standard SharePoint Online sites the Get-SPOSite PowerShell Cmdlet can be used which returns the property StorageUsageCurrent that reports how much storage the Site Collection is consuming. Unfortunately Get-SPOSite cannot be used with OneDrive sites however this information can be obtained using CSOM and I put together the script below to demonstrate this.

Simply update the highlighted variables with the URL of the OneDrive to query and an admin user who has access. The script will output the total storage used (in MBs) to the console.

$SiteURL = “https://tenant-my.sharepoint.com/personal/first_lasttenant_onmicrosoft_com”
$User = "admin@tenant.onmicrosoft.com"
$Password = Read-Host -Prompt "Enter password" -AsSecureString

$Assemblies = (
    "Microsoft.SharePoint.Client, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c",
    "Microsoft.SharePoint.Client.Runtime, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c",
    "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
)
$CSharp = @"
using Microsoft.SharePoint.Client;
using System.Collections.Generic;
using System.Linq;
public static class QueryHelperLinq
{
    public static void LoadSiteUsage(ClientContext ctx, Microsoft.SharePoint.Client.Site site)
    {
        ctx.Load(site, s => s.Usage);
    }
}
"@

Add-Type -ReferencedAssemblies $Assemblies -TypeDefinition $CSharp -Language CSharp;
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
$Site = $Context.Site
[QueryHelperLinq]::LoadSiteUsage($Context, $Site)
$Context.ExecuteQuery()
Write-Host $SiteURL "is using" ([Decimal]::Round($Site.Usage.Storage /1MB)) "MB Storage" -ForegroundColor Green

Brendan Griffin - @brendankarl