Managing user accounts in a Microsoft 365 environment often requires filtering users based on specific attributes such as their Job Title. Microsoft Graph PowerShell provides a powerful way to query and manage users efficiently. In this article, we will explore how to retrieve users based on their job title using the Get-MgUser cmdlet.
# Connect to Microsoft Graph PowerShell
Connect-MgGraph -Scopes "User.Read.All"
# Define the job title to filter users
$JobTitle = "IT Manager"
# Retrieve users with the specified job title
$Users = Get-MgUser -All -Filter "jobTitle eq '$JobTitle'" -Property Id, DisplayName, JobTitle | Select-Object Id, DisplayName, JobTitle
# Display results
$Users | Format-Table -AutoSize
The script can be extended in various ways to make it more dynamic and useful for real-world administration. Here are a few enhancements:
$JobTitle = Read-Host "Enter the job title to filter users"
$Users | Export-Csv -Path "FilteredUsers.csv" -NoTypeInformation
Get-MgUser -All -Search "jobTitle:Manager" -Property Id, DisplayName, JobTitle
if ($Users.Count -eq 0) {
Write-Host "No users found with job title '$JobTitle'" -ForegroundColor Yellow
} else {
$Users | Format-Table -AutoSize
}
The ability to filter Microsoft 365 users by job title is particularly useful in several administrative scenarios, including:
Error | Cause | Solution |
---|---|---|
Request_UnsupportedQuery | -Filter does not support case-insensitive searches | Ensure you provide an exact match or use -Search |
Property 'jobTitle' not found | API response does not contain jobTitle field | Ensure you specify -Property JobTitle in the query |
Authorization_RequestDenied | Insufficient permissions | Ensure the account has User.Read.All permission |
Filtering users by job title using Graph PowerShell is a powerful way to streamline user management in Microsoft 365. By leveraging Get-MgUser with the -Filter parameter, administrators can efficiently retrieve specific users based on their job roles. Additionally, further enhancements like exporting data to CSV, allowing dynamic user input, and handling multiple job titles can make this script even more useful for day-to-day administration.
© m365corner.com. All Rights Reserved. Design by HTML Codex