Managing user licenses in Microsoft 365 is crucial for ensuring that resources are allocated efficiently and that users have access to the services they need. In this article, we will explore a Graph PowerShell script that lists both licensed and unlicensed users in a table format. We will delve into how the script works, how it can be enhanced, and discuss potential errors and solutions.
Below is the Graph PowerShell script that lists licensed and unlicensed users separately:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All"
# Retrieve all users and their assigned licenses
$allUsers = Get-MgUser -All -Property "Id,DisplayName,UserPrincipalName,AssignedLicenses"
# Filter licensed and unlicensed users
$licensedUsers = $allUsers | Where-Object { $_.AssignedLicenses.Count -gt 0 }
$unlicensedUsers = $allUsers | Where-Object { $_.AssignedLicenses.Count -eq 0 }
# Display licensed users in a table format
Write-Host "Licensed Users:"
$licensedUsers | Select-Object DisplayName, UserPrincipalName, ID | Format-Table -AutoSize
# Display unlicensed users in a table format
Write-Host "`nUnlicensed Users:"
$unlicensedUsers | Select-Object DisplayName, UserPrincipalName, ID | Format-Table -AutoSize
Script Output:
Note:: You can look for a specific user's license details by running Get-MgUserLicenseDetail -UserId <UPN>
The script can be enhanced in various ways to provide more functionality and flexibility:
$licensedUsers | Select-Object DisplayName, UserPrincipalName, Department | Format-Table -AutoSize
$unlicensedUsers | Select-Object DisplayName, UserPrincipalName, Department | Format-Table -AutoSize
# Export licensed users to CSV
$licensedUsers | Select-Object DisplayName, UserPrincipalName | Export-Csv -Path "LicensedUsers.csv" -NoTypeInformation
# Export unlicensed users to CSV
$unlicensedUsers | Select-Object DisplayName, UserPrincipalName | Export-Csv -Path "UnlicensedUsers.csv" -NoTypeInformation
try {
Connect-MgGraph -Scopes "User.Read.All"
}
catch {
Write-Host "Failed to connect to Microsoft Graph. Please check your credentials and permissions." -ForegroundColor Red
exit
}
try {
$allUsers = Get-MgUser -All -Property "Id,DisplayName,UserPrincipalName,AssignedLicenses"
}
catch {
Write-Host "Failed to retrieve users. Please check your network connection and permissions." -ForegroundColor Red
exit
}
Error | Solution |
Authentication failed. Please check your credentials. | Ensure that the account used has the necessary permissions (User.Read.All) and that multi-factor authentication (MFA) is properly configured. |
Insufficient privileges to complete the operation. | Verify that the account has the required permissions. You may need to grant additional permissions or use an account with higher privileges. |
Failed to connect to Microsoft Graph | Check your internet connection and ensure that your firewall or proxy settings are not blocking the connection to Microsoft Graph. |
$licensed | Export-Csv -Path "LicensedUsers.csv" -NoTypeInformation
$unlicensed | Export-Csv -Path "UnlicensedUsers.csv" -NoTypeInformation
Where-Object
works well for smaller environments.-Filter
parameter with assignedLicenses/$count
(and -ConsistencyLevel eventual
) to perform filtering on the server side.
This Graph PowerShell script provides a simple yet effective way to list licensed and unlicensed users in Microsoft 365. By understanding how the script works and exploring ways to enhance it, you can tailor it to better fit your organization’s needs. Additionally, being aware of potential errors and their solutions can help you troubleshoot issues effectively, ensuring smooth operation.
With this script, administrators can gain valuable insights into their licensing status, helping to optimize resource allocation and ensure compliance with licensing requirements.
© m365corner.com. All Rights Reserved. Design by HTML Codex