How can I get a total of active and inactive O365 Users?


I’m trying to work out how to get an output of the number of active and inactive O365 Users from a Tenant.

All I need is the total number of active (have logged on within the last 30 days) and inactive (have not logged on within the last 30 days) Users,

I know how to pass credentials to O365 tenant using PowerShell.

I know how to get a csv out of the O365 Tenant with the UserPrincipalName and LastLogonDate. If fact I used the script from here to achieve that. Browse code samples | Microsoft Docs

This needs to be automated so that on the last day of the month it is ran and the output sent via an email to a non technical person. For that I’ll use Task Scheduler and a service account. They do not want a CSV file, just the two numbers.

Is there something already out there that I’ve not been able to find, or will I need to use something like an “if, else if, else” statement or a ForEach Object command?

Sorry if this seems simple to you but I’m still trying to learn.


Kind Regards, Darren

You have proven you can hit the MS to get pre-built scripts for your use case, but you only got the one. There are far more there and complete modules for O365 management.
O365 already provides a report sit that you can hit from your browser.

Office 365 License, User and Last Logon report Since Office 365 does not have a good report to show when your users last logged on and how many licenses are really in used, instead of 'assigned',I've built the following (simple) powershell script to fetch you a nicely formatted CSV report that will tell you: which licenses Download : LicReport365_v0.5.ps1

Export Office 365 User Last Logon Date Time Reports to CSV(EXO\SPO\ODFB\SFB\AAD)
Using this tool, you can generate Office 365 User Last Logon time report(Exchange\SharePoint\OneDrive\Yammer\Skype\Azure AD). Also you can Export Office 365 User Last login time to CSV or other file formats.
Download :

This tool generates the last logon time report from Activity Management API and also from their mailbox.
Following reports are available with this tool.

  • All User Logon Activity
  • Users not logged in for last N days
  • Mailbox Logon Activity
  • Mailbox Users not logged in for last N days

Apart from reporting the user last logon time, following Office 365 user management actions are also supported.

  • Managing the user license
  • Reset user password
  • Allow user sign-in access
  • Block user sign-in access

There are many more examples of what you are after all over the web as well, since this is a really common thing.


Hello postanote,

Thank you for your response.

I think it is exactly what I’m after :slight_smile:

I will do some testing today.

Kind Regards, Darren