Export licensed & blocked users using PowerShell

When you think about it, having users licensed in Office 365, while they can’t login, it’s not a very good use of money.
Maybe you need to keep the license for the mailbox/data not to go away, or maybe the user left the company and you blocked his/her access for security reasons (change the password for better security) while you still need to retrieve the data?

But there may be some scenarios where you need to export the users who are licensed AND blocked in order to free licenses.
This blog post will do just that!

The full script is available on my TechNet Gallery, amongst some others scripts that can (hopefully) help you.

Anyways, let’s get started.


Minimum requirements:

  • O365 credentials
  • Permissions to run PowerShell scripts


Create the Help section

Best practice when scripting, is to help as much as possible the recipient of the script.

If you are using the PowerShell ISE (and you should!), then you can find templates by typing CTRL + J and choose one that will help with your intended script.
This way you won’t need to start from scratch…


CTRL + J to display templates



Help section


Add mandatory parameter(s)

Then, we can add mandatory parameter(s) to force the recipient of the script to enter a location where to put the export file?
And also indicate what format the script expect, with a little help message?

OK, let’s do that too.


Mandatory=$true + Help message


Connect to Office 365

In order to retrieve the information we want, we first need to log into Office 365.
Store your credentials in a variable, to use this variable with the Connect-MsolService cmdlet.


Connecting to Office 365


Finally, ask for the information, and export to a .csv file


Get-MsolUser and export to .csv file


Save and Run the script


Run script and connect to O365


The .csv file will only contain the users that match both conditions.


Comments are closed.

%d bloggers like this: