Generate a Department Headcount Report via Microsoft Graph

Prev Next

Query Entra ID to produce a sorted breakdown of users per department across your tenant.

Complexity: Beginner. Impact: read-only + reporting + low-risk.

Why use this workflow

Department headcount reports are a foundational HR and IT operations artifact. They are useful for license planning, org chart validation, onboarding capacity planning, identifying users with missing or malformed department attributes, and reconciling Entra ID data with HRIS systems like Workday or BambooHR.

Because the department property is a free-text field on the Entra user object, this report is also a quick way to surface data hygiene issues: typos, inconsistent casing, or users whose department was never populated at all.

When to run it

  • Monthly, as part of an identity hygiene review
  • Before a reorg or cost-center realignment
  • Whenever HR asks "how many people are in X?" and you want a definitive answer from the source of truth
  • Before bulk license assignments scoped by department

Expected outcomes

  • A sorted list of every distinct department value in Entra ID and the count of users in each
  • A count of users with no department assigned (reported as (Unassigned))
  • A total user count for the tenant
  • Optional CSV output suitable for sharing with HR or importing into Excel or Power BI

Risks and considerations

Things to watch for

  • Free-text inconsistency. "Engineering", "engineering", and "Eng" are counted as three separate departments. Consider normalizing before you report.
  • Guest and service accounts. The default query returns all users, including guests and non-human accounts. Filter by userType eq 'Member' and accountEnabled eq true if you want only active employees.
  • Pagination. Large tenants paginate. Always follow @odata.nextLink to avoid under-reporting.
  • Privacy. While headcount totals are generally non-sensitive, some organizations treat department-level staffing data as confidential. Handle the output accordingly.
  • Do not use this report as authoritative headcount for finance or legal purposes without reconciling against your HRIS. Entra is only as accurate as the provisioning process that populates it.

Required permissions

PermissionWhy it's needed
User.Read.AllRequired to enumerate all users in the tenant and read the department property
Directory.Read.AllAlternative broader permission that also grants read access to user attributes

Ask Dex

Paste the prompt into a Dex conversation.

Prompt for IT admins

Show me a breakdown of departments and the number of people in each, across the whole tenant. Include users with no department set and give me a total headcount.

Manual steps (admin center)

The admin center does not offer a one-click "group by department" report, but you can approximate it by exporting and pivoting in Excel.

Step 1: Open the user list

Go to the Microsoft 365 admin center > Users > Active users.

Step 2: Export all users

  1. Select Export users at the top of the Active users list.
  2. Choose Export all users and confirm. A CSV is downloaded (or emailed for large tenants).

Step 3: Pivot in Excel

  1. Open the exported CSV and locate the Department column.
  2. Select the data range and insert a PivotTable.
  3. Drag Department into both Rows and Values (as Count).
  4. Sort the pivot table descending by count.

Step 4: Clean up

Scan for typos, case inconsistencies, and blank departments. Flag these for remediation with HR or your provisioning system.

For larger tenants, the API or PowerShell approaches below are faster and produce cleaner output.

Microsoft Graph API

The report is built from a single Graph endpoint with $select to minimize payload size, followed by client-side aggregation.

Endpoint

GET https://graph.microsoft.com/v1.0/users?$select=department&$top=999

Example request

GET /v1.0/users?$select=department&$top=999
Authorization: Bearer <token>
ConsistencyLevel: eventual

Example response (truncated)

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(department)",
  "@odata.nextLink": "https://graph.microsoft.com/v1.0/users?$select=department&$skiptoken=...",
  "value": [
    { "department": "Engineering" },
    { "department": "Sales" },
    { "department": null }
  ]
}

Pagination

Follow the @odata.nextLink URL until it is no longer returned. Each page returns up to 999 users when $top=999 is specified (the maximum allowed).

Filtering to active members only (optional)

GET /v1.0/users?$select=department&$filter=userType eq 'Member' and accountEnabled eq true&$count=true&$top=999
ConsistencyLevel: eventual

The ConsistencyLevel: eventual header and $count=true parameter are required when you filter on accountEnabled.

Aggregation

Graph does not support server-side GROUP BY. Aggregate client-side by iterating the combined result set and incrementing a dictionary keyed on department (treat null or empty as (Unassigned)).

PowerShell

The Microsoft Graph PowerShell SDK handles pagination automatically with -All, which makes this a short query plus a Group-Object.

Prerequisites

  • PowerShell 7 or later is recommended
  • Microsoft.Graph module installed: Install-Module Microsoft.Graph -Scope CurrentUser
  • An account with at least User.Read.All

Connect

Connect-MgGraph -Scopes "User.Read.All"

Pull users and group by department

# Fetch only the department property for speed
$users = Get-MgUser -All -Property Department

# Group and sort
$report = $users |
    ForEach-Object { if ([string]::IsNullOrWhiteSpace($_.Department)) { '(Unassigned)' } else { $_.Department } } |
    Group-Object |
    Select-Object @{N='Department';E={$_.Name}}, Count |
    Sort-Object Count -Descending

$report | Format-Table -AutoSize

Export to CSV

$report | Export-Csv -Path ".\department-headcount.csv" -NoTypeInformation