Tag users matching an email-domain filter with a Custom Security Attribute value for governance and Conditional Access scoping.
Complexity: Intermediate. Impact: identity-governance + bulk-operation + conditional-access + attribute-based-access-control.
Why use this workflow
Custom Security Attributes (CSAs) in Microsoft Entra ID are key-value pairs that you define at the tenant level to classify directory objects (users, service principals, devices). They are commonly used to drive Conditional Access scoping, attribute-based access control (ABAC) on Azure resources, license governance, and HR reporting.
A frequent operational task is the bulk assignment of a CSA value to a cohort of users, for example tagging every employee of a specific domain with Accounts > UserType = Employee so that they fall under the correct Conditional Access policy or ABAC role assignment. Assigning the value one user at a time in the Entra admin center is tedious and error-prone. This workflow shows the three paths you can use.
Run this workflow whenever you:
- Onboard a new acquired domain and need to tag its users with a classification value.
- Roll out a new CSA set and need to backfill existing users.
- Periodically reconcile the CSA against a source of truth (HRIS, domain, group membership).
Expected outcomes
- Every user matching the target filter (for example, mail ending in
@contoso.com) carries the specified Custom Security Attribute value. - A reconciliation report that separates users who already had the attribute from users who were newly assigned.
- An audit trail of the
PATCH /beta/users/{id}calls, which you can review in the Entra audit log. - Confidence that Conditional Access policies, ABAC roles, or reports keyed on that attribute scope the tagged cohort correctly.
Risks and considerations
- A privileged role is required. Assigning CSA values requires the Attribute Assignment Administrator role (or an equivalent custom role). Global Administrator alone is not sufficient. Grant the operator this role before you run the PATCH calls.
- Beta endpoint. Custom Security Attribute read and write operations are on
/beta. The payload shape may change when they move tov1.0. - PATCH merges, it does not replace. A partial
customSecurityAttributespayload merges into the existing attribute set. To remove a value, sendnullexplicitly. - Predefined values. If the attribute is configured with
usePreDefinedValuesOnly = true, you must use an allowed value withisActiveset to true. Free text fails. - Advanced queries.
endsWith()requires theConsistencyLevel: eventualheader and$count=true. Without them, the filter returns HTTP 400. - Do not run the bulk assignment without first dry-running the filter and reviewing the user list. A typo in the domain filter can tag the wrong cohort.
- Compliance. CSAs can hold data subject to internal governance (for example, employment classification). Confirm who can read the attribute set before you populate it. Visibility is governed by attribute-set-level permissions.
Required permissions
| Permission | Why it's needed |
|---|---|
| CustomSecAttributeDefinition.Read.All | Read the CSA definitions and allowed values to validate the attribute name and target value before assignment. |
| CustomSecAttributeAssignment.ReadWrite.All | Read and write Custom Security Attribute values on user objects. |
| User.Read.All | Query users by mail filter to build the target cohort. |
| Attribute Assignment Administrator (directory role) | Required in addition to the Graph scopes. The signed-in principal must hold this role to persist CSA writes. |
Ask Dex
Paste the prompt into a Dex conversation.
Prompt for IT admins
Assign the Custom Security Attribute {attribute_set}.{attribute_name} = "{value}" to every user whose primary email ends with {mail_suffix}. Skip users who already have the value, produce a report of updated vs. skipped users, and confirm the assignments afterward.
Manual steps (admin center)
Use this path for small cohorts, or as a one-off verification after a scripted run.
Step 1: Confirm the attribute definition
- Sign in to the Microsoft Entra admin center as an Attribute Definition Reader (or higher).
- Go to Protection > Custom security attributes.
- Open the attribute set (for example,
Accounts) and verify that the target attribute (for example,UserType) exists and that the desired value (for example,Employee) is listed as an active allowed value.
Step 2: Build the user cohort
- Go to Identity > Users > All users.
- Select Add filter, choose User principal name or Email, and enter the matching fragment (for example,
@contoso.com). - Export the filtered list with Download users so that you have a record of who was in scope.
Step 3: Assign the attribute per user
- Select a user in the filtered list to open their profile.
- Select the Custom security attributes tab.
- Select Add assignment.
- Choose the Attribute set (for example,
Accounts), the Attribute name (for example,UserType), and the Value (for example,Employee). - Select Save.
- Repeat for each user in the cohort.
Step 4: Verify
- Return to Users > All users, select Columns, and add Custom security attributes.
- Confirm that all targeted users display the expected attribute value.
For more than a handful of users, use the Graph API or PowerShell paths below. The admin center does not offer bulk selection for CSA assignment.
Microsoft Graph API
All CSA operations are on the /beta endpoint.
1. List attribute definitions (validate the target)
GET https://graph.microsoft.com/beta/directory/customSecurityAttributeDefinitionsResponse (abbreviated):
{
"value": [
{
"id": "Accounts_UserType",
"attributeSet": "Accounts",
"name": "UserType",
"type": "String",
"status": "Available",
"usePreDefinedValuesOnly": true
}
]
}2. List allowed values for the attribute
GET https://graph.microsoft.com/beta/directory/customSecurityAttributeDefinitions/Accounts_UserType/allowedValuesConfirm that the target value (for example, Employee) is present with isActive: true.
3. Query the target users (advanced query)
GET https://graph.microsoft.com/v1.0/users?$filter=endsWith(mail,'@contoso.com')&$select=id,displayName,mail&$count=true
ConsistencyLevel: eventualThe ConsistencyLevel: eventual header and $count=true are required for endsWith().
4. PATCH each user to assign the attribute
PATCH https://graph.microsoft.com/beta/users/{user-id}
Content-Type: application/json
{
"customSecurityAttributes": {
"Accounts": {
"@odata.type": "#microsoft.graph.customSecurityAttributeValue",
"UserType": "Employee"
}
}
}Expected response: HTTP 204 No Content.
5. Verify
GET https://graph.microsoft.com/beta/users/{user-id}?$select=displayName,customSecurityAttributesNotes
- Graph does not support
$filteroncustomSecurityAttributes. Retrieve the candidates and filter client-side. - PATCH merges at the attribute-set level. Sending
UserType: nullclears only that attribute. SendingAccounts: nullclears the entire set. - Use
$batchto group PATCH calls and reduce round-trips.
PowerShell
PowerShell can automate the full workflow: authenticate, validate the attribute definition, enumerate the target cohort, skip users who are already tagged, and assign the CSA to the rest with a per-user success and failure report.
Prerequisites
- PowerShell 7 or later
- The
Microsoft.Graphmodule installed (Install-Module Microsoft.Graph -Scope CurrentUser) - The signed-in admin must hold the Attribute Assignment Administrator directory role
Approach
- Connect to Graph with the minimum scopes required:
Connect-MgGraph -Scopes "User.Read.All","CustomSecAttributeDefinition.Read.All","CustomSecAttributeAssignment.ReadWrite.All" - Read the attribute definition and validate
usePreDefinedValuesOnly. If it is true, confirm that the supplied value is an active allowed value. - Query all users matching
endsWith(mail, $MailSuffix)with the correctConsistencyLevelheader. - Split the cohort into users who already have the value and users who need the assignment.
- Send a PATCH per user, collect the outcomes, and write a CSV report.
- Support a preview mode (for example,
-WhatIf) so that you can review the changes before you apply them.