Export Office 365 Users to CSV using PowerShell

We can easily export users using Office 365 Admin center UI, but in this way we can get only set of attributes and we can’t easily filter our required set of users. In this article, I am going to write powershell script to export all office 365 users and export only licensed users to csv file.

Note: Before proceed, Install and Configure Azure AD PowerShell

Use the below command to export all the office 365 users. You can add more columns to export.

Get-MsolUser -All  | Select-Object UserPrincipalName, DisplayName, isLicensed |
Export-CSV "C:\all-office-365-users.csv" -NoTypeInformation -Encoding UTF8

In some scenarios, you might want to get details of only licensed office 365 users. To get only licensed users, you need to apply where filter in the output of Get-MsolUser cmdlet.

Get-MsolUser -All | Where-Object { $_.isLicensed -eq ”TRUE” } | 
Select-Object UserPrincipalName, DisplayName, Department  |
Export-CSV "C:\licensed-office-365-users.csv" -NoTypeInformation -Encoding UTF8

You can also extract what are the licenses has been assigned to every users. The below command export all the licensed users and applied license details to csv file.

Get-MSOLUser -All | Where-Object { $_.isLicensed -eq ”TRUE” } | 
Select-Object UserPrincipalName, DisplayName, {$_.Licenses.AccountSkuId} |
 Export-CSV "C:\office-365-users-license.csv" -NoTypeInformation -Encoding UTF8

Note: Checkout this post ( Manage Office 365 License using Powershell
) to add a new license, remove an existing license and update existing license options (enable or disable license plans) using PowerShell command.


Advertisement

Leave a Comment