Collecting deleted Office 365 users license status with PowerShell

There are several health check and compliance items you can think of when it comes to checking your Office 365 subscription. For example, you can check if all admins have MFA enabled or not. Similarly, you can check Office 365 subscription status to ensure that all licenses are in use and you are not paying for licenses not in use. One of the items you may also want to check is to make sure that deleted Office 365 users are not using any Office 365 licenses. It is obvious that you do not want deleted users to remain with an Office 365 license. In this article, we provide a PowerShell script that you can use to check if any deleted users are using Office 365 licenses. This is one of the routines checks I perform for all my customers to ensure I can highlight the need for deactivating licenses of deleted users.

deleted office 365 users

Before executing the PowerShell Script

Before executing the PowerShell script provided in the later section of this article, make sure you have installed Exchange Online modules by using the “Install-Module MSOnline” and make sure you have global administrator access before the script can connect and retrieve the required data. You will also be required to create a C:\Temp directory on the computer from where you run the PowerShell script.

The PowerShell script provided in this article performs operations such as importing the PowerShell modules required to connect to Office 365 MSOnline. The script uses Connect-MSOnline to connect to Office 365 and provide a login prompt to connect to Office 365. The script collects deleted user accounts from Office 365 and then checks which users are licensed among the deleted. The script will generate two CSV files for reporting purposes; C:\Temp\TestResult.CSV file, which contains the numbers and C:\Temp\Data\EXCH_Data.CSV, which contains the actual deleted users and if any of these users are using Office 365 licenses.

PowerShell script

Executing this PowerShell script will generate a report in CSV format. The report file can be found at C:\Temp\TestResult.CSV and the data file can be found under C:\Temp\Data folder. You need to provide your Office 365 connect credentials.

Remove-Item $TestCSVFile -ErrorAction SilentlyContinue
Import-Module MSOnline
$ThisString="Total Users, Total Users In Recycle Bin, Unlicensed, Licensed, Percentage Users In Recycle Bin, Data File"
Add-Content "$TestCSVFile" $ThisString
Remove-Item $DataFileLocation -ErrorAction SilentlyContinue
$AllItems = Get-MsolUser -All
$AllItemsCount = $AllItems.Count
$TotNot = 0
$TotYes = 0
$TotPercentage = 0
$TotNotItems = Get-MsolUser -All -ReturnDeletedUsers | Select-Object UserPrincipalName, DisplayName, IsLicensed
$TotNotCount = $TotNotItems.DisplayName.Count
$TotLicensedNow = 0
ForEach ($Item in $TotNotItems)
IF ($Item.IsLicensed -eq $true)
$TotNotLicensedNow = 0
ForEach ($Item in $TotNotItems)
IF ($Item.IsLicensed -eq $false)
#$TotYesItems = Get-MsolUser -All | Where-Object {$_.UsageLocation -ne $null}
#$TotYesCount = $TotYesItems.Count
$ValSTR = $AllItemsCount.ToString()+","+$TotNotCount.ToString()+","+$TotNotLicensedNow.ToString()+","+$TotLicensedNow.ToString()+","+$TotPercentage.ToString()+","+$DataFileLocation
Add-Content "$TestCSVFile" $ValSTR
$TotNotItems | Export-CSV $DataFileLocation -NoTypeInformation
IF ($TotNotCount -ne 0)
IF ($TotPercentage -gt 15)
$SumVal = ""
$TestText="HIGH ISSUE"
$SumVal = ""
$SumVal = ""
$TestText = "PASSED ITEMS"

Once the PowerShell script has finished executing you can open C:\Temp\TestResult.CSV file. It will contain the actual numbers as shown in the screenshot below, which is taken from O365 IT Health & Risk Scanner.

Deleted Office 365 users

As you can see in the screenshot, the script reported total users, total users in Office 365 Recycle Bin, total unlicensed, and licensed among the deleted users. It also lists the percentage of users in the Office Recycle bin. The data file can be accessed at C:\Temp\Data\EXCH_Data.CSV, which contains the actual user names.

Important: The above PowerShell script was retrieved from O365 IT Health & Risk Scanner, which can perform about 120 checks in Office 365 to ensure your Office 365 services are health and your organization is meeting the compliance standards.

Deleted Office 365 users and license status

In this article, we provided a PowerShell script that can be used to collect deleted Office 365 users and to check how many users are licensed among those deleted. The script requires that you connect to Office 365 MSOnline services and provide the global administrator or equivalent credentials. The script collects the data and results are provided in the two CSV files, which can be found under C:\Temp and C:\Temp\Data folders.

Featured image: Pixabay

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top