Filtering Microsoft 365 Users by Job Title Using Graph PowerShell

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.

PowerShell Script to Filter Users by Job Title


# 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
                            

How the Script Works

  1. Connect to Microsoft Graph: The script starts by establishing a connection to Microsoft Graph with the User.Read.All permission.
  2. Define the Job Title: The variable $JobTitle stores the job title to filter users (e.g., "IT Manager").
  3. Filter Users Using -Filter Parameter: The Get-MgUser cmdlet queries users where the jobTitle property matches the specified value.
  4. Optimize Output: The script fetches only the required properties (Id, DisplayName, JobTitle) to improve performance.
  5. Format and Display Results: The retrieved users are displayed in a table format for easy readability.

Further Enhancements

The script can be extended in various ways to make it more dynamic and useful for real-world administration. Here are a few enhancements:

  • Allow User Input for Dynamic Filtering: Instead of hardcoding the job title, prompt the user to enter it dynamically:
  • $JobTitle = Read-Host "Enter the job title to filter users"
  • Export Results to CSV for Reporting: Store the filtered users into a CSV file for later analysis:
  • $Users | Export-Csv -Path "FilteredUsers.csv" -NoTypeInformation
  • Search for Partial Matches: The -Filter parameter only supports exact matches. To perform a broader search, use -Search:
  • Get-MgUser -All -Search "jobTitle:Manager" -Property Id, DisplayName, JobTitle
  • Enhance Error Handling: Add logic to handle cases where no users match the filter:
  • if ($Users.Count -eq 0) {
        Write-Host "No users found with job title '$JobTitle'" -ForegroundColor Yellow
    } else {
        $Users | Format-Table -AutoSize
    }

Use Cases

The ability to filter Microsoft 365 users by job title is particularly useful in several administrative scenarios, including:

  1. Generating Departmental Reports: If an administrator wants to generate a report on all employees in a specific role (e.g., "Software Engineers"), this script provides an efficient way to retrieve such data.
  2. Managing Access Controls: IT admins can use job title-based filtering to apply security policies, ensuring that only users with specific job roles have access to sensitive resources.
  3. Bulk License Assignments: Organizations may need to apply different license plans to employees based on their job titles. Filtering users by job title makes this process more streamlined.
  4. Automating Onboarding & Offboarding Processes: When a new hire joins or an employee leaves, automated scripts can ensure they are placed in the correct groups, assigned appropriate access, or deactivated efficiently based on their role.
  5. Filtering Employees for Communications: HR or IT teams can extract a list of users by job title to send targeted email communications or alerts.

Possible Errors & Solutions

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

Conclusion

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