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

Comments (3)

  1. Anonymous says:

    Great Post , Brendan!!! indeed the script has helped me a lot.

  2. Nice! Thanks Gary 🙂

  3. Here's another approach which avoids the dynamic type creation (I use the assemblies installed as part of the SharePoint Online Management Shell but you could change the path to point to those installed by the redistributable package as in your sample
    - also, Get-SPOSite does work with the onedrive site so not sure what issues you were running into):

    [System.Reflection.Assembly]::LoadFile("C:Program FilesSharePoint Online Management ShellMicrosoft.Online.SharePoint.PowerShellMicrosoft.SharePoint.Client.dll") | Out-Null
    [System.Reflection.Assembly]::LoadFile("C:Program FilesSharePoint Online Management ShellMicrosoft.Online.SharePoint.PowerShellMicrosoft.SharePoint.Client.Runtime.dll") | Out-Null

    $cred = Get-Credential
    $spoCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password)
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext "https://tenant-my.sharepoint.com/personal/username_tenant_onmicrosoft_com"
    $ctx.Credentials = $spoCred
    http://blogs.technet.com/b/fromthefield/archive/2015/02/17/report-onedrive-storage-using-powershell.aspx#
    $site = $ctx.Site
    $ctx.Load($site)
    $ctx.ExecuteQuery()
    $site.Retrieve("Usage")
    $ctx.ExecuteQuery()
    $site.Usage

Skip to main content