PowerShell Script to Retrieve M365 User License Info
In this post, Im excited to share a handy PowerShell script that allows you to retrieve license information for users in Microsoft 365 (M365). This script will help administrators efficiently manage their users licenses and ensure compliance across the organization.
Before we dive into the script, dont forget to check out our software, ServerEngine, at [ServerEngine](https://serverengine.co), where you can find powerful tools to streamline your server management tasks.
### Step 1: Connect to Microsoft 365
First, we need to authenticate and establish a session with Microsoft 365 using the MSOnline module. Make sure you have the module installed on your system.
“`powershell
# Install MSOnline module (if not already installed) Install-Module -Name MSOnline -Force -AllowClobber # Import the module and connect Import-Module MSOnline $credential = Get-Credential Connect-MsolService -Credential $credential
“`
This step prompts you for your M365 admin credentials and establishes a session using the `Connect-MsolService` command.
### Step 2: Retrieve Users and Their License Details
Next, we will retrieve a list of users along with their assigned license details. The `Get-MsolUser` command will be used for this purpose.
“`powershell
# Retrieve users and their license details $users = Get-MsolUser -All | Select-Object DisplayName, UserPrincipalName, Licenses $users
“`
This code extracts the users display name, user principal name (email), and the licenses assigned to them, enabling you to see their licensing status.
### Step 3: Format and Display the Information
Lastly, we will present the license information in a readable format. This will help you quickly identify users who may be lacking licenses or those that have multiple licenses assigned.
“`powershell
# Display the formatted license information $users | ForEach-Object { $licenseInfo = if ($_.Licenses.Count -gt 0) { ($_.Licenses | ForEach-Object { $_.AccountSkuId }) -join , } else { "No License Assigned" } [PSCustomObject]@{ Display Name = $_.DisplayName Email = $_.UserPrincipalName License Info = $licenseInfo } } | Format-Table -AutoSize
“`
This will generate a well-structured table that displays each users display name, email, and licensing information, making it easy for admins to review.
### Conclusion
This simple yet powerful script enables Microsoft 365 administrators to effortlessly manage user licenses. For more such PowerShell scripts and to discover tools that can simplify your server management, visit [ServerEngine](https://serverengine.co). Stay tuned for more helpful scripts and tips!