Export a list of unused Office 365 licenses in your delegated administration tenants

If you’re managing a few Office 365 customers, it can be difficult to keep track of license usage without browsing the partner portal, or logging into the tenant directly.

Using PowerShell, you can perform bulk tasks across all of the tenants that you have delegated admin access to.

This script will export a CSV with the license count and consumption for all of your customers and all of their subscriptions. It’ll also export a total of the unused licenses so you can make sure your customers aren’t being billed unnecessarily.

How to export a CSV of unused Office 365 licenses in your customers’ tenants

  1. You’ll need to make sure that you have the Azure Active Directory PowerShell Module installed on your computer. Follow our quick guide here if you don’t already have it.
  2. To run the script below, copy and paste it into either Notepad or PowerShell ISE.
  3. If you’re using Notepad, just save it with a .ps1 extension (eg. filename.ps1) and then drag it on top of a PowerShell window. If you’re using PowerShell ISE, just copy it into a new document and click the Run Script button (or press F5).
  4. You’ll need to make sure that the path specified by $msolAccountSkuCsv actually exists. You can update this to whatever you like, or just create a folder called temp under C:\.
  5. You can also replace [email protected] with the username for your Delegated Admin account.
Clear-Host
$UserName = "[email protected]"
$Cred = get-credential -Credential $UserName
Connect-MSOLService -Credential $Cred
Import-Module MSOnline

$clients = Get-MsolPartnerContract -All

$msolAccountSkuResults = @()
$msolAccountSkuCsv = "C:\temp\LicenseList.csv"

ForEach ($client in $clients) {

$licenses = Get-MsolAccountSku -TenantId $client.TenantId

foreach ($license in $licenses){

$UnusedUnits = $license.ActiveUnits - $license.ConsumedUnits

$licenseProperties = @{
TenantId = $client.TenantID
CompanyName = $client.Name
PrimaryDomain = $client.DefaultDomainName
AccountSkuId = $license.AccountSkuId
AccountName = $license.AccountName
SkuPartNumber = $license.SkuPartNumber
ActiveUnits = $license.ActiveUnits
WarningUnits = $license.WarningUnits
ConsumedUnits = $license.ConsumedUnits
UnusedUnits = $unusedUnits
}

Write-Host "$($License.AccountSkuId) for $($Client.Name) has $unusedUnits unused licenses"
$msolAccountSkuResults += New-Object psobject -Property $licenseProperties
}

}

$msolAccountSkuResults | Select-Object TenantId,CompanyName,PrimaryDomain,AccountSkuId,AccountName,SkuPartNumber,ActiveUnits,WarningUnits,ConsumedUnits,UnusedUnits | Export-Csv -notypeinformation -Path $msolAccountSkuCsv

Write-Host "Script complete"

When run, you should see the output in the PowerShell Window. PowerShell Unused Office 365 Licenses In Delegated Administration Tenants Script

In this example, the CSV Output will be located in C:\temp\LicenseList.csv. The CSV will look like this and will make it easy to identify unassigned licenses in the UnusedUnits column.Exported CSV of Unused Office 365 Licenses In Delegated Administration Tenants

Was this article helpful?

Related Articles