Export Office 365 Users to CSV using PowerShell

Export Office 365 Users to CSV using PowerShell

by Morgan

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.

12Get-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.

123Get-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.

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