Managing license assignments in Microsoft 365 is crucial for ensuring that users have access to the tools they need while optimizing costs. The Get-MgUser cmdlet retrieves user information, while the Get-MgUserLicenseDetail cmdlet provides detailed insights into the licenses and service plans assigned to users. By combining these cmdlets, administrators can efficiently audit license usage, generate detailed reports, and identify optimization opportunities.
This article explains how to pair these cmdlets to manage license assignments effectively.
Example 1: List All Users with Their Assigned Licenses
# Retrieve all users and their license details
$users = Get-MgUser -All
foreach ($user in $users) {
$licenses = Get-MgUserLicenseDetail -UserId $user.Id
foreach ($license in $licenses) {
[PSCustomObject]@{
UserPrincipalName = $user.UserPrincipalName
SkuId = $license.SkuId
SkuPartNumber = $license.SkuPartNumber
ServicePlans = ($license.ServicePlans | Select-Object -ExpandProperty ServicePlanName) -join ", "
}
}
}
By looping through all users retrieved with Get-MgUser, the script collects license details for each user. This enables administrators to build a comprehensive audit report.
Example 2: Export License Assignments for All Users to a CSV File
# Export user license details to a CSV
$users = Get-MgUser -All
$licenseReport = @()
foreach ($user in $users) {
$licenses = Get-MgUserLicenseDetail -UserId $user.Id
foreach ($license in $licenses) {
$licenseReport += [PSCustomObject]@{
UserPrincipalName = $user.UserPrincipalName
DisplayName = $user.DisplayName
SkuPartNumber = $license.SkuPartNumber
AssignedPlans = ($license.ServicePlans | Select-Object -ExpandProperty ServicePlanName) -join ", "
}
}
}
$licenseReport | Export-Csv -Path "UserLicenseDetails.csv" -NoTypeInformation
Exporting data to a CSV allows you to analyze the data further using Excel or share it with stakeholders.
$users = Get-MgUser -Filter "Department eq 'IT'" -All
ServicePlans = if ($license.ServicePlans) {
($license.ServicePlans | Select-Object -ExpandProperty ServicePlanName) -join ", "
} else {
"No Service Plans"
}
Error | Cause | Solution |
---|---|---|
Insufficient privileges to complete the operation. | Missing permissions like User.Read.All or User.ReadWrite.All. | Grant the required permissions in Azure AD for the application or account running the script. |
The user does not have any licenses assigned. | The user doesn’t have a license. | Add these users to a "No License Assigned" report for further review. |
Request is too large to process. | Fetching too much data at once. | Retrieve users in smaller batches or apply specific filters to limit the dataset. |
Combining Get-MgUser and Get-MgUserLicenseDetail is a powerful way to manage and audit license assignments in Microsoft 365. These cmdlets allow you to identify licensing inefficiencies, export detailed reports, and ensure proper license allocation across your organization. Start using these techniques today to simplify your license management workflows and optimize costs.
© m365corner.com. All Rights Reserved. Design by HTML Codex