It can sometimes be useful to get a list of Office 365 users with a specific license type via PowerShell. Instead of logging into the Office 365 portal and using a filtered view in the admin center, you can do it straight from the command line.
- Connect to Office 365 via Powershell. If this cmdlet doesn’t work for you, follow this quick guide for instructions on installing the required PowerShell module.1
Connect-MsolService
How to Install the Azure Active Directory PowerShell Module via PowerShell
- Open the Start menu on your computer and search for ‘Powershell’
- Right-click on Windows PowerShell and choose ‘Run as administrator’
- Type the following command and press enter.1
Install-Module
-Name
MSOnline
- Type “Y” to install and import the NuGet provider
- Type “Y” again to trust the provider
- Wait for the package to install, then type the following to enter your Office 365 admin credentials and connect to Azure Active Directory via PowerShell:1
Connect-MsolService
- Once the Azure Active Directory PowerShell module has been installed, you only need to run the Connect-MsolService command to connect to the Azure AD service on this PC.
- Run Get-MsolAccountSku to get a list of the current licenses in your Office 365 tenant. Make a note of the AccountSkuId value for the license you want to filter on.1
Get-MsolAccountSku
- Now you can edit this short script to get the users matching that license. In this case, we’re getting users with the EnterprisePremium license.1
Get-MsolUser
| Where-Object
{($_.licenses).AccountSkuId -match
"EnterprisePremium"}
Replace EnterprisePremium with the AccountSkuID you’re trying to filter by. Since we’re using the -match operator we don’t need to type the entire AccountSkuID, we can just type enough of it to ensure that we’re only retrieving that specific one.
Export these users to a text document
You can export these users to a text document using the Out-File cmdlet.
1 | Get-MsolUser | Where-Object {( $_ .licenses).AccountSkuId -match "EnterprisePremium" } | Out-file C:\temp\EnterprisePremiumUsers.csv |
Export all licesensed users to file
Get-MsolUser -All | where {$_.isLicensed -eq $true} | select Displayname, userprinciplename,islicensed, {$.Licenses.AccountSuID}| Export-CSV C:\O365Userlist.csv -NoTypeInformation