OneDrive licensing is an important aspect of managing Microsoft 365 subscriptions. It determines user storage limits, available features, and compliance with organizational policies. Using PnP PowerShell, administrators can efficiently retrieve and audit OneDrive licensing information for users in their Microsoft 365 environment.
Step 1: Install & Update PnP PowerShell
Before executing any commands, ensure PnP PowerShell is installed and up to date:
Install-Module -Name PnP.PowerShell -Force -AllowClobber
To update the module:
Update-Module -Name PnP.PowerShell
Step 2: Connect to Microsoft 365 Using PnP PowerShell
To retrieve OneDrive licensing details, connect to Microsoft 365 using PnP PowerShell:
Connect-PnPOnline -Scopes "Directory.Read.All" -Interactive
If you prefer app-based authentication, use:
$tenantId = "your-tenant-id"
$clientId = "your-client-id"
$clientSecret = "your-client-secret"
Connect-PnPOnline -Tenant $tenantId -ClientId $clientId -ClientSecret $clientSecret -Url "https://graph.microsoft.com"
Step 3: Retrieve OneDrive Licensing Information for All Users
To list all users and their assigned licenses, use:
Get-PnPAzureADUser | Select-Object DisplayName, UserPrincipalName, AssignedLicenses
This displays a list of users along with their assigned Microsoft 365 licenses.
Step 4: Filter Users With OneDrive Licenses
To check which users have OneDrive included in their Microsoft 365 license, filter the results:
Get-PnPAzureADUser | Where-Object { $_.AssignedLicenses -match "OneDrive" } | Select-Object DisplayName, UserPrincipalName
This helps identify users with OneDrive access.
Step 5: Get Detailed License Information for a Specific User
To retrieve the exact licenses assigned to a user, replace "user@domain.com"
with the actual email:
Get-PnPAzureADUser -Identity "user@domain.com" | Select-Object DisplayName, UserPrincipalName, AssignedLicenses
This provides a detailed view of all licenses linked to the user, including OneDrive.
Step 6: Export OneDrive Licensing Data to CSV
To generate a report of all users and their OneDrive licensing status, export the data:
Get-PnPAzureADUser | Select-Object DisplayName, UserPrincipalName, AssignedLicenses | Export-Csv -Path "C:\Reports\OneDrive_Licensing_Report.csv" -NoTypeInformation
This creates a CSV file with OneDrive license details, which can be used for audits.
Step 7: Identify Users Without OneDrive Licenses
To find users without OneDrive licenses, run:
Get-PnPAzureADUser | Where-Object { $_.AssignedLicenses -notmatch "OneDrive" } | Select-Object DisplayName, UserPrincipalName
This helps administrators identify gaps in licensing and provision missing OneDrive access.
Step 8: Assign OneDrive License to a User
If a user lacks a OneDrive license, you can assign it manually. First, get the License SKU ID:
Get-PnPAzureADSubscribedSku | Select-Object SkuId, SkuPartNumber
Look for OneDrive or Microsoft 365 licenses in the results. Then, assign the license:
Set-PnPAzureADUserLicense -Identity "user@domain.com" -AddLicenses "SkuId" -RemoveLicenses @()
Replace “SkuId” with the actual ID for OneDrive.
Step 9: Remove OneDrive License from a User
If you need to revoke OneDrive access:
Set-PnPAzureADUserLicense -Identity "user@domain.com" -AddLicenses @() -RemoveLicenses "SkuId"
This prevents the user from accessing OneDrive.
Step 10: Automate OneDrive Licensing Reports Using Task Scheduler
To schedule a weekly licensing audit, save the following script as OneDrive_Licensing_Audit.ps1:
Connect-PnPOnline -Scopes "Directory.Read.All" -Interactive
$reportPath = "C:\Reports\OneDrive_Licensing_Audit.csv"
Get-PnPAzureADUser | Select-Object DisplayName, UserPrincipalName, AssignedLicenses | Export-Csv -Path $reportPath -NoTypeInformation
$adminEmail = "admin@yourdomain.com"
$subject = "OneDrive Licensing Audit Report"
$body = "Attached is the latest OneDrive Licensing Report."
Send-MailMessage -To $adminEmail -From "noreply@yourdomain.com" -Subject $subject -Body $body -Attachments $reportPath -SmtpServer "smtp.office365.com" -UseSsl -Port 587 -Credential (Get-Credential)
To schedule the script:
- Open Task Scheduler
- Click Create Basic Task
- Choose Weekly and set the preferred time
- Select Start a Program → Browse to OneDrive_Licensing_Audit.ps1
- Click Finish