How we can Automatiacally add users to entra with IDM role and other permission.

2026-11-19T09:25:50.05+00:00

I need to add some Jenkins users to entra with one IDM role and one more role for them.

How we can add them automatically if we have csv file.

Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments

1 answer

Sort by: Most helpful
  1. Vasileios Dionysopoulos 826 Reputation points
    2026-11-19T09:59:48.86+00:00

    Hi Gagan,

    I'm assuming "IDM" and the second role are app roles on your Jenkins enterprise application (the roles you see under Enterprise applications → Jenkins → Users and groups → Assign). If they're something else, let me know.

    With Microsoft Graph PowerShell, one script can create each user from the CSV, or reuse the user if it already exists, and assign both roles.

    users.csv

    DisplayName,UserPrincipalName,MailNickname
    Jenkins User 1,******@contoso.com,jenkins.user1
    Jenkins User 2,******@contoso.com,jenkins.user2
    

    Script (run as a User Administrator plus an Application Administrator or Cloud Application Administrator)

    Connect-MgGraph -Scopes "User.ReadWrite.All","AppRoleAssignment.ReadWrite.All","Application.Read.All"
    
    $appName   = "Jenkins"              # enterprise app display name
    $roleNames = @("IDM", "Developer")  # app role display names to assign
    
    $sp    = Get-MgServicePrincipal -Filter "displayName eq '$appName'"
    $roles = $sp.AppRoles | Where-Object { $_.DisplayName -in $roleNames }
    
    foreach ($row in Import-Csv .\users.csv) {
        $user = Get-MgUser -Filter "userPrincipalName eq '$($row.UserPrincipalName)'" -ErrorAction SilentlyContinue
        if (-not $user) {
            $user = New-MgUser -DisplayName $row.DisplayName -UserPrincipalName $row.UserPrincipalName `
                -MailNickname $row.MailNickname -AccountEnabled `
                -PasswordProfile @{ Password = "<REDACTED-random-initial-value>"; ForceChangePasswordNextSignIn = $true }
        }
        foreach ($role in $roles) {
            New-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $sp.Id `
                -PrincipalId $user.Id -ResourceId $sp.Id -AppRoleId $role.Id | Out-Null
            Write-Host "$($row.UserPrincipalName) -> $($role.DisplayName)"
        }
    }
    

    Replace the placeholder with a randomly generated initial value, and give users their first sign-in through SSPR or a Temporary Access Pass rather than sending passwords around.

    If you have Entra ID P1, there's a simpler long-term option: assign both roles once to a group such as Jenkins-IDM, and have the script only add users to that group (New-MgGroupMember).

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.