Get Office 365 users with a specific license type via PowerShell

Get Office 365 users with a specific license type via PowerShell. Sometimes it is helpful to obtain a list of Office 365 users with a specific license type via PowerShell. There is no need to log into Office 365 portal and use a filtered view in the admin center. We can go straight into it from the command line.

 

In this post, we will be discussing how to get a list of users with a specific license type in Powershell.

What are Office 365 Licenses?

Office 365 Licenses are licenses that are specific to the Office 365 platform, they grant permission to various or specific Office 365 services. These varieties of Licensing Options provide Office 365 customers with the ability to pay for services as required.

Office 365 License Types

There are several Office 365 License Types and it is important to understand them as this will save money and ensure employee productivity.

Below is a list of some of the common Office 365 Licenses available:
  • Office 365 E1
  • Microsoft 365 E1
  • Office 365 E3
  • Microsoft 365 E3
  • Office 365 E5
  • Microsoft 365 E5
  • Office 365 F3
  • Microsoft 365 Business Basic (formerly Office 365 Business Essentials)
  • Microsoft 365 Business Standard (formerly Office 365 Business Premium)
  • Microsoft 365 Business Premium (formerly Microsoft 365 Business)
  • Enterprise Mobility + Security E3
  • Azure Active Directory Premium P1
  • Azure Information Protection Premium P1
  • Exchange Online (Plan 1)
  • Exchange Online Kiosk
  • OneDrive for Business (Plan 2)
  • SharePoint (Plan 1
  • Visio Plan 1
  • Project Plan 1
  • Microsoft Intune
  • Power BI Premium P1

In our article Get Office 365 users with a specific license type via PowerShell (Best Practices) we shall talk about creating reports with Office 365 via Powershell.

Create Report of Office 365 users with a specific License Type using PowerShell

We can generate a list of Office 365 users with a specific License Type from the Office 365 Administrator Portal but this will not be covered. Before we use PowerShell to generate this report, we will need to understand the following:

Product Name: This is the License name displayed in the Office 365 Administration Portal. Below is an image to explain it better:

String ID: This is the name or identifier that is used by Office 365 PowerShell v1.0 cmdlets when performing operations on Licenses, it is also known as the SkuPartNumber.

Below are the corresponding String IDs (SkuPartNumbers) for the Product Names in the image above:
Product Name String ID
Azure Information Protection Premium P1
RIGHTSMANAGEMENT
Business Apps (free)
SMB_APPS
Dynamics 365 P1 Trial for Information Workers
DYN365_ENTERPRISE_P1_IW
Microsoft 365 E3
SPE_E3
Microsoft Dynamics AX7 User Trial
AX7_USER_TRIAL
Microsoft Power Automate Free
ERP_INSTANCE
Microsoft Teams Exploratory
TEAMS_EXPLORATORY
Power BI (free)
POWER_BI_STANDARD

Microsoft provides a table containing the majority of Office 365 Licenses with their Product Names, String IDs, GUID, Service Plan Names, Service Plan IDs and Service Plan Friendly Names. 

Later in the article how to get Office 365 users with a specific license type via PowerShell , we will learn how to get the String IDs of the Licenses on the Office 365 tenant using PowerShell.

We will be making use of Windows Microsoft PowerShell and the MSOnline module which is known as the MSOnline V1 in PowerShell module for Azure Active Directory.

Azure Active Directory (Azure AD) is a cloud based identity and access management service. This tool helps employees access external resources, like Microsoft 365, the Azure portal and loads of other SaaS applications. 

This module can be found in the PowerShell Gallery and this gives us access to create a more detailed report using PowerShell CMDLETs.

Installing the MSOnline Module

To gain access to this module, first, we would have to install the module on the PC we are going to use to generate the report.

Launch the Windows PowerShell in Administrator mode and then run the scripts below:

The first script will be setting the execution policy of the PowerShell Process to Unrestricted. This is necessary to avoid any restrictions.
				
					# set the execution policy of the process
Set-ExecutionPolicy -Scope Process Unrestricted -Force
				
			
The next script will be to install the MSOnline Module.
				
					# Install MSOnline Module
Install-Module -Name 'MSOnline' -Force
				
			

Importing the MSOnline Module

Next, we will be importing the installed MSOnline Module into the current PowerShell session using the cmdlet below:
				
					# Import MSOnline Module
Import-Module -Name 'MSOnline'-Force
				
			
We will be making use of these two CMDLETS imported from the MSOnline module:
  • Get-MsolUser: This cmdlet gets the users from Azure Active Directory
  • Get-MsolAccountSku: This cmdlet returns the list of License SKUs on the Office 365 Tenant.

Connecting to MsolService

We need to connect to Azure AD before we can query it using the CMDLETS listed above.

The PowerShell script below allows this to be possible:

				
					# Connect to MsolService
Connect-MsolService
				
			
Next, there will be a pop-up dialogue box to enter the global administrator credentials:

Getting a list of the current licenses on a Tenant

Some Administrators might want to get an overview and usage of the licenses or a list of license String IDs (SkuPartNumbers) on their Office Tenant before they run the report.
Below is a cmdlet that gives an overview and usage of the licenses on the Tenant:
				
					#  Returns an overview of license usage on the tenant
Get-MsolAccountSku
				
			
Below is a cmdlet that gives the list of the SKUs (String IDs) of licenses on the tenant
				
					#  Returns all the SKUs (String IDs) of licenses on the tenant
(Get-MsolAccountSku).SkuPartNumber
				
			

Exporting Office 365 Users with a specific license type to CSV using PowerShell

In this section how to Get Office 365 users with a specific license type via PowerShell is to create a report for Office 365 Users with a specific license type.

The report will have the following parameters in a CSV output file:
  • Display Name
  • UserPrincipalName
  • First Name
  • Last Name
  • When Created
  • Mobile Number
  • Department
  • City
  • Usage Location
  • MFA Status
  • Last Password Change Timestamp
  • Block Sign-In Status

Below is the PowerShell script that generates this Office 365 Users CSV report:

Note: Change the $StringID variable in the PowerShell script to match the String ID of the license you which to run the report and also change the $ReportName variable to suit your desired output file path and file name.

The “.\” represents the present working directory (you can get this information by running the following cmdlets in your PowerShell session: PWD or Get-Location).

“.\” can be changed to any desired directory on the machine (E.g C:\Windows\)

				
					# set the name of the report alongside the path ".\ relative path"
$ReportName = ".\Office365LicenseReport.csv"

# Set the String ID of the license ("SPE_E3" = Microsoft 365 E3)
$StringID = "SPE_E3"

# get all office Users
$UsersDetails = Get-MsolUser -All | Where-Object {($_.Licenses).AccountSkuId -match $StringID}

# set export array
$ExportArray = @()

# loop through each office 365 user with the specified String ID
Foreach ($UserDetail in $UsersDetails) {

    # set the office 365 user's UserPrincipalName
    $UserUpn = $UserDetail.UserPrincipalName.ToString()

    # add values to array
    $ExportArray += [PSCustomObject][Ordered]@{

        # get the office 365 user's display name
        "Display Name"                   = $UserDetail.DisplayName

        # get the office 365 user's userprincipalname
        "UserPrincipalName"              = $UserUpn

        # get the office 365 user's first Name
        "First Name"                     = $UserDetail.FirstName

        # get the office 365 user's last Name
        "Last Name"                      = $UserDetail.LastName

        # get the office 365 user's creation date
        "When Created"                   = $UserDetail.WhenCreated

        # get the office 365 user's mobile number
        "Mobile Number"                  = $UserDetail.MobilePhone

        # get the office 365 user's department
        "Department"                     = $UserDetail.Department

        # get the office 365 user's city
        "City"                           = $UserDetail.City

        # get the office 365 user's usage location
        "Usage Location"                 = $UserDetail.UsageLocation

        # get the office 365 user's mfa status
        "MFA Status"                     = $UserDetail.StrongAuthenticationRequirements.state

        # get the office 365 user's last password change timestamp
        "Last Password Change Timestamp" = $UserDetail.LastPasswordChangeTimestamp

        # get the office 365 user's block sign-in status
        "Block Sign-In Status"           = $UserDetail.BlockCredential

    }
}

# echo the exported array
Write-Output $ExportArray 

# export to csv file
$ExportArray | Export-Csv -Path $ReportName -Delimiter "," -NoTypeInformation
				
			

Great! We have learned about Get Office 365 users with a specific license type via PowerShell. We will summarize. 

Get Office 365 users with a specific license type via PowerShell Conclusion

In this article, we discussed Office 365 Licenses and covered steps on how to generate a report of users with a specific license type. Check out our Office 365  reporting tool that allows you to easily create Office 365 license reports and automate these steps easily.

Avatar for Esemuede Okougbo
Esemuede Okougbo

My background is in Cloud Operations, Office 365, Exchange Online, Security & Compliance, Active Directory and Dynamics 365 F&O. I'm a PowerShell Developer and ATTUNE Evangelist. Certified Cyber Security Professional (CSFPC).

3.5 2 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x