Finding OneDrive for Business space usage using PowerShell
One of the SharePoint Online administrator of the customer I work with wanted to know the space used by a business user’s private OneDrive.
The following PowerShell script helps to know the exact same information.
The script assumes that you have the SharePoint administrator role in your tenant.
To run this script “SharePoint Online Management Shell” PowerShell module should have been installed
Make sure you update the variables $tenantName & $url
Please note that the below scripts use minimal error handling for simplicity and clean code.
Script for single URL
PowerShell
x
16
1
Clear-Host
2
3
$tenantName = "tenant name"
4
$url = "https://unit4-my.sharepoint.com/personal/<user one drive url>"
5
6
Connect-SPOService -Url "https://$tenantName-admin.sharepoint.com"
7
8
$sc = Get-SPOSite $url -Detailed -ErrorAction SilentlyContinue | Select-Object url, storageusagecurrent, Owner
9
$usage = [math]::round(($sc.StorageUsageCurrent/1024),2)
10
$owner = $sc.Owner
11
12
Write-Host "Site: $url"
13
Write-Host "Owner: $owner"
14
Write-Host "Usage: $usage GB"
15
16
Disconnect-SPOService
The following script helps to do the same for multiple URLs. All you need is a text file with OneDrive URL per line and it will provide output in a csv file.
Script for multiple URLs
PowerShell
1
22
1
$inputFileName = "OneDrive-Usage-URLs.txt"
2
$outputFileName = "OneDrive-Usage-URLs.csv"
3
4
Clear-Host
5
$tenantName = "tenant name"
6
7
Connect-SPOService -Url "https://$tenantName-admin.sharepoint.com"
8
$inputFile = Get-Content -Path .\$inputFileName
9
Add-Content .\$outputFileName -Value "URL,Owner,Usage (GB)"
10
11
foreach ($url in $inputFile) {
12
$sc = Get-SPOSite $url -Detailed -ErrorAction SilentlyContinue | Select-Object url, storageusagecurrent, Owner
13
$usage = [math]::round(($sc.StorageUsageCurrent / 1024), 2)
14
$owner = $sc.Owner
15
Add-Content .\$outputFileName -Value "$url,$owner,$usage"
16
Write-Host "Site: $url"
17
Write-Host "Owner: $owner"
18
Write-Host "Usage: $usage GB"
19
Write-Host ""
20
}
21
22
Disconnect-SPOService
Input file format (OneDrive-Usage-URLs.txt)
Plain Text
1
1
https://tenant-my.sharepoint.com/personal/user1_tenant_com
2
https://tenant-my.sharepoint.com/personal/user2_tenant_com
Output file format (OneDrive-Usage-URLs.csv)
Plain Text
1
1
URL,Owner,Usage (GB)
2
https://tenant-my.sharepoint.com/personal/user1_tenant_com,user1@tenant.com,123.45
3
https://unit4-my.sharepoint.com/personal/user2_tenant_com,user2@tenant.com,678.90