msonline

Office 365 : Reclaiming licenses

Migrated to Office 365? Running low on licenses and want to do a review of existing license assignments?

In work, we recently had to do a reclamation of licenses from users who had left the company but had somehow managed to retain their licensing status in Office 365.

To help with this I created a powershell script that would get a list of disabled Active Directory accounts who had a UPN (UserPrincipalName) that was formatted for use in Office 365 and cross reference that with the list of licensed users in Microsoft Online Services. Surprisingly Microsoft haven't included something like this in the existing views. I'm sure it'll be added in an upgrade...

Behold the results! Make sure you change the UPN value being queried below as I'd be surprised if ye're using DOMAIN.COM! All comments and suggestions on improvements are welcome. I'm relatively new to Powershell.

Import-Module ActiveDirectory
Import-Module msonline

Connect-MsolService -Credential (Get-Credential)

#$ErrorActionPreference = $SilentlyContinue

# Create counter and results array
[int]$i = 0
$results = @()

# Query AD for all accounts with UPN needed for Office365 that are disabled and save to array
$disabledUsers = Get-ADUser -Filter ({UserPrincipalName -like "*@DOMAIN.COM" -AND enabled -eq $false}) | select UserPrincipalName

# For all entries in $disabledUsers, query their licensing status on MS Online and save to $results
for ($i=0; $i -le ($disabledUsers.count-1); $i++) {
	$results += get-msoluser -UserPrincipalName $disabledUsers[$i].UserPrincipalName -ErrorAction SilentlyContinue
}
# For all $results where isLicensed is TRUE, output the DisplayName and License status
for ($i=0; $i -le ($results.count-1); $i++){
	if ($results[$i].isLicensed -eq "True"){
		$results[$i] | select DisplayName, IsLicensed 
	}
}